Reputation: 2299
I am constructing a vector X
in Matlab by concatenating the outcomes of each iteration in a loop procedure.
What I am doing at the moment is
X=[];
for j=1:N
%do something that delivers a vector A
%X=[X;A]
end
It is not possible to predict a priori the size of A. Is there any way in which I can preallocate the space?
Upvotes: 2
Views: 432
Reputation: 19689
A possible solution:
A=cell(1,N); %Pre-allocating A instead of X as a cell array
for k=1:N %I changed the name of loop variable since `j` is reserved for imag numbers
%Here I am generating a matrix of 5 columns and random number of rows.
%I kept number of columns to be constant (5) because you used X=[X;A]
%in the question which means number of columns will always be same
A{k} = rand(randi([1,10],1,1),5); %doing something that delivers a vector A
end
X = vertcat(A{:});
Upvotes: 6