If N not too large to cause memory problems, you can preassign output to a vector of the same size as input , and remove all unnecessary elements at the end of the loop.
output = NaN(N,1); for i=1:N ... output(i) = input(i); ... end output(isnan(output)) = [];
There are two alternatives.
If output is too large, if it was assigned size N , or if you did not know the upper limit of output size, you can do the following
lengthOutput = 100; output = NaN(lengthOutput,1); counter = 1; for i=1:N ... output(counter) = input(i); counter = counter + 1; if counter > lengthOutput %
Finally, if N is small, it is quite normal to call
output = []; for i=1:N ... output = [output;input(i)]; ... end
Note that performance deteriorates dramatically if N gets large (say> 1000).
Jonas source share