storing a 100x100 matrix from each iteration in a single new matrix

15 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

dpb
dpb el 6 de Mzo. de 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 comentarios
Luca Wright
Luca Wright el 6 de Mzo. de 2021
just to check, where you have "yourfunction", do i have the output that i am pulling out of the function, or the actual function name? When i put the actual function name it gives me an error, but its happy with the output!
dpb
dpb el 6 de Mzo. de 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...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by