storing a 100x100 matrix from each iteration in a single new matrix
16 views (last 30 days)
Show older comments
I am running a loop 100 times, and within that i produce a 100x100 matrix (this represents topography, each loop is a pperiod in time).
I would like to store every outut of that loop into a single matrix, as well as its assigned iteration number.
i.e. 100, 100x100 matricies, all assigned to their iteration number.
Topography, 1
Topoography, 2 , etc all in one matrix
I am led to beleive this is possible, but have no idea how to do it!
0 Comments
Accepted Answer
dpb
on 6 Mar 2021
Two options -- either use a cell array each element of which is a 2D array or a 3D array in which each plane is one time step. Being a fan of using cell arrays only when necessary, I'd most likel choose to go that route which would look like --
N=100; % set the counter in a variable, don't bury "magic" numbers in code -- can change just data, not code
T=zeros(N,N,N); % preallocate for speed
for i=1:N
T(:,:,i)=yourfunction(i); % compute the output arrays, store by plane
end
3 Comments
dpb
on 6 Mar 2021
You assign the result of the function, presuming the function returns the aforementioned and preallocated 2D array.
If your function returns something different, then whatever is is that is done to get the 2D array needs to be done before assignment.
To diagnose whatever error you actually get, would have to see it in context with the code the generated the error and have a definition of what the function returns...
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!