Storing multiple matrices in an array

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

1 commentaire

Stephen23
Stephen23 le 20 Juin 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,...

Connectez-vous pour commenter.

Réponses (1)

Adam
Adam le 20 Juin 2017
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 commentaires

John F.
John F. le 20 Juin 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 le 20 Juin 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 le 20 Juin 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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Question posée :

le 20 Juin 2017

Commenté :

le 20 Juin 2017

Community Treasure Hunt

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

Start Hunting!

Translated by