Build matrix of different size column vectors generated inside a for loop
16 views (last 30 days)
Show older comments
Hi,
Inside a for loop, I am generating column vectors of different lengths that are being plotted against time for a number of datasets.
How do I take these different sized column vectors and store them in a matrix/ table/ array, such that the number of rows is equal to the longest column vector, and the number of columns is equal to the number of iterations in the for loop?
Where I have got up to:
for u = 1:length(listings)
ForceNext(u) = ForceTrunc(u);
ForceAll = zeros(75, 1);
ForceAll{:,u} = [ForceAll, ForceNext(u)]
end
I have also attached the full script, with lines 127 - 156 relating to the question. I will later take mean values of each row of the matrix I wish to create, and plotting the subsequent vector against a time series.
Thanks for any and all help
3 Comments
Bob Thompson
on 27 Jan 2021
Is the size captured in one of your other dataset variables? If the size is captured somewhere else you can use that value to more organically specify the size of the array.
Accepted Answer
Stephen23
on 27 Jan 2021
Edited: Stephen23
on 27 Jan 2021
Inside the loop store the vectors in a preallocated cell array. Then after the loop use PADCAT (download required):
To get you started (pseudocode):
N = number of iterations
C = cell(1,N);
for k = 1:N
... your code
C{k} = output vector
end
M = padcat(C{:});
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!