Storing multiple matrices in an array
Show older comments
How do I store multiple matrices of identical dimensions but different names in one array?
1 Comment
Stephen23
on 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,...
Answers (1)
Adam
on 20 Jun 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 Comments
John F.
on 20 Jun 2017
Adam
on 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
on 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?
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!