Storing Multiple Matrices from a For Loop

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.

 採用された回答

Cedric
Cedric 2013 年 4 月 7 日
編集済み: Cedric 2013 年 4 月 7 日

46 投票

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 件のコメント

Anthony Uwaechia
Anthony Uwaechia 2015 年 8 月 17 日
this is actually an intelligent way of saving matrix from loop iterations. thanks a lot. it really solved my problem
Swathi Chandrasekar
Swathi Chandrasekar 2017 年 5 月 9 日
Amazing.Thanks
Alvina Khairun Nisa
Alvina Khairun Nisa 2017 年 5 月 23 日
Can I save m{k} to .mat file?
Stephen23
Stephen23 2017 年 5 月 23 日
@Alvina Khairun Nisa: allocate it to a temporary variable and save that, e.g.:
for k = 1:N
data = m{k};
fnm = sprintf('file_%d.mat',k);
save(fnm,'data');
end
Irfan Azhar
Irfan Azhar 2018 年 1 月 2 日
very helpful. thanks indeed.
Mohammad Kifayath Chowdhury
Mohammad Kifayath Chowdhury 2019 年 7 月 4 日
Thanks a lot.
Redouane Bouchou
Redouane Bouchou 2020 年 4 月 7 日
thank you , that was very helpfeul
Himanshu Banait
Himanshu Banait 2020 年 7 月 13 日
Thanks a lot.
Malik haseeb Haider
Malik haseeb Haider 2020 年 12 月 8 日
many thanks
mary john
mary john 2020 年 12 月 31 日
thanks a lot
Marcio Pereira
Marcio Pereira 2021 年 3 月 21 日
Very good! Grateful!
Lukas Leitenbacher
Lukas Leitenbacher 2021 年 11 月 29 日
You´re the real MVP, man! Thx a lot for this approach!
Ali Berenjian
Ali Berenjian 2022 年 4 月 3 日
thanks a lot!
Bryan Guilcapi
Bryan Guilcapi 2022 年 7 月 20 日
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 2023 年 7 月 22 日
thank you

サインインしてコメントする。

その他の回答 (2 件)

Saeed Bello
Saeed Bello 2017 年 8 月 7 日

7 投票

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 件のコメント

Irfan Azhar
Irfan Azhar 2018 年 1 月 2 日
many thanks indeed.
ELIAS PERATICOS
ELIAS PERATICOS 2018 年 4 月 27 日
Saeed Bello, thanks a lot this was very useful.
rees adah
rees adah 2018 年 10 月 31 日
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?

サインインしてコメントする。

D.K. Rao
D.K. Rao 2017 年 7 月 1 日

0 投票

Thank you very much Cedric Wannaz

カテゴリ

質問済み:

2013 年 4 月 7 日

コメント済み:

2023 年 7 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by