Storing multiple matrices in an array

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

1 Comment

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,...

Sign in to comment.

Answers (1)

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 Comments

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,...?
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.
@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?

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 20 Jun 2017

Commented:

on 20 Jun 2017

Community Treasure Hunt

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

Start Hunting!