Storing Multiple Matrices from a For Loop

47 vues (au cours des 30 derniers jours)
Shane
Shane le 7 Avr 2013
Commenté : Walid le 22 Juil 2023
How do I index and store multiple matrices as a run through a for loop? For instance, I generate a 10x10 matrix the first time through and I want to store this result for access later before proceeding to the next matrix generation.

Réponse acceptée

Cedric
Cedric le 7 Avr 2013
Modifié(e) : Cedric le 7 Avr 2013
You can use a cell array, e.g
n = 10 ;
M = cell(n, 1) ;
for k = 1 : n
M{k} = 20*k + rand(10) ;
end
you can see that each
M{1}, M{2}, etc
is a the 10x10 matrix defined at a specific iteration of the loop.
Note the difference between regular block indexing with () and accessing cells content with {}:
M(1)
is cell #1 of the cell array M, whereas
M{1}
is the content of cell #1 (in the present case, it is a 10x10 matrix).
  15 commentaires
Bryan Guilcapi
Bryan Guilcapi le 20 Juil 2022
Hi, i have a question, is possible to plot all this cell in one plot calling the data that are inside of the cell?
Walid
Walid le 22 Juil 2023
thank you

Connectez-vous pour commenter.

Plus de réponses (2)

Saeed Bello
Saeed Bello le 7 Août 2017
You can use a three-dimensional matrix e.g.
for i = 1:8 % no. of iteration
S(:, :, i) = myfunx(i,10); % 10 x 10 output
end
Then you can access each iteration by calling S(:, : , 1) or S(:, : , 2) or S(:, : , 3) and so on. Source:https://stackoverflow.com/questions/30036908/output-of-for-loop-as-a-matrix-matlab
  3 commentaires
ELIAS PERATICOS
ELIAS PERATICOS le 27 Avr 2018
Saeed Bello, thanks a lot this was very useful.
rees adah
rees adah le 31 Oct 2018
I also have a similar problem but this solution didn't work for me.i intend to store the matrices coming from a nested for loop into a multidimensional array and reference it later...how do I do that please?

Connectez-vous pour commenter.


D.K. Rao
D.K. Rao le 1 Juil 2017
Thank you very much Cedric Wannaz

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by