Storing multiple matrices in an array

How do I store multiple matrices of identical dimensions but different names in one array?

1 Kommentar

Stephen23
Stephen23 am 20 Jun. 2017
Well, actually there is a way to access those variables in a loop, but you might like to first know what the MATLAB documentation says about it: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
You might also like to read what experienced MATLAB users say about what you are trying to do (hint: they strongly advise against it):
A much better solution is to load your data into one variable, and then simply access the data using indexing and/or fieldnames, e.g. if you use load then always load into an output variable. This will more efficient, neater, more robust, easier to check, easier to debug, faster,...

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Adam
Adam am 20 Jun. 2017

0 Stimmen

Depends how you want them to end up. You can pack them into a numeric array of one higher dimension or a cell array (in which it doesn't matter if they are the same size).
A numeric array is always better.
e.g.
A = rand(20,30);
B = rand(20,30);
C = cat( 3, A, B );

3 Kommentare

John F.
John F. am 20 Jun. 2017
Thank you! Yes I would like to store them in a numeric array, then extract the mean of each matrix. is there a way to call all the matrices (500) without writing them out all A,B,C,...?
Adam
Adam am 20 Jun. 2017
Well, once they are in C you just have 1. How you get hold of them in the first place I don't know, but if you store them directly into the 3d array (with a better name than C) then they are just indices along the 3rd dimension rather than names.
Jan
Jan am 20 Jun. 2017
@John F: Of course, but it is your turn to know, how your 500 matrices are stored. What about creating a [n x m x 500] array?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 20 Jun. 2017

Kommentiert:

am 20 Jun. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by