4

I am new to programming and I was wondering if my question has a simple implementation. I have a bunch of matrices and I want a way to be able to store them, or be able to easily call them and do operations on them. For example, if I have 100 matrices, called, M1,M2,...M100; is there a way I can rename them so that if I want to call the nth matrix, I can just write M(nth)?

EDIT: For example, if I want to add M1+M1, M1+M2, ...,M1+M100; I want to be able to write a loop something kind of like,

for i=1:100 AM(i)=M(1)+M(i) end

Is this possible?

user2540462
  • 61
  • 1
  • 2
  • 5
  • Have you created `M1, ..., M100` with `eval()`? – Oleg Jul 01 '13 at 21:12
  • ...so you created 100 matrices one at a time? – Dan455 Jul 01 '13 at 21:18
  • yes. and now I want to be able to call them using some sort of short hand, so instead of M1, I want to rename it M(1). Is this possible? – user2540462 Jul 01 '13 at 21:23
  • My goodness... well if you really have that kind of patience, you can do `M = {M1 M2 M3 ... M100};`, but yes you have to type out all 100 of them. Then, as innoSPG said in their answer, M{i} will give you the matrix you desire. – Dan455 Jul 01 '13 at 21:31
  • Okay, I see, thank you Dan445. I was missing the part about defining M={M1...M100}. Thanks again! – user2540462 Jul 01 '13 at 21:36
  • Do you mind if I ask why you are creating 100 matrices one at a time? Where are their values coming from? And why do you need 100 of them? – Dan455 Jul 01 '13 at 21:39
  • I have a program that I am using that spits them out 1 at a time an gives them various names. I have renamed them M1, ... M100. I agree that this is not very efficient, but I am working on understanding how to implement this more efficiently. – user2540462 Jul 01 '13 at 21:58
  • @Dan455 He can do it in a loop, with merely patience required... `for i=1:n M{i}=eval(['M' num2str(i)]) end` – Adiel Jul 01 '13 at 21:58
  • 1
    @Adiel This is exactly what needs to be avoided in the first place! – Oleg Jul 01 '13 at 22:10
  • 1
    @user2540462 I recommend modifying your program, that spits the matrices in random names to create one cell array, and spit them into separate cells. – Oleg Jul 01 '13 at 22:11
  • @OlegKomarov what's wrong? – Adiel Jul 01 '13 at 22:29
  • 1
    @Adiel http://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html – Dan455 Jul 02 '13 at 00:02
  • @Dan455 It's important post, thanks. But i didn't find there a solution for this situation. Except, of course, the manual trivial way you suggested. – Adiel Jul 02 '13 at 08:14
  • 2
    This situation has to be avoided in block at the core, i.e. avoiding popping incremental variables into the workspace. This is a clear example that `var1, var2,...` is not a *sustainable* approach when you try to scale up – Oleg Jul 02 '13 at 09:49

1 Answers1

12

Use cell array

AM = cell(1,100);

and set it as

AM{i} = Mi;

then you can access it as

AM{i};

note the use of {} to access each element of the cell array AM, that is in turn a matrix

innoSPG
  • 4,588
  • 1
  • 29
  • 42