How to create a vector?

8 views (last 30 days)
Sim on 16 Oct 2012
Hello everyone, Suppose with a for loop I get an array for each file (array could be of different lengths for each file). I want to get a column vector which will be merging of these arrays. So all the values of arrays from each file will be in this huge vector. Thank you for you help in advance.

Accepted Answer

Babak on 16 Oct 2012
Edited: Babak on 16 Oct 2012
for j=1:n
% find newone that comes from file(j)
vector = [vector , newone]
Matt Kindig
Matt Kindig on 16 Oct 2012
Keep in mind that if this is a "huge vector", then it might be rather slow due to a lack of memory pre-allocation. I would recommend something like this:
% vector is output from file(j)
r = size(vector,1);
Vector = NaN(r,n); %this is the pre-allocation step-- very important for memory management/efficiency!
for j=2:n,
%newone comes from file(j)
Vector(:,j) = newone;

Sign in to comment.

More Answers (0)


Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!