0

I would like to create variables and name the variables through an index in a loop into the name of the variable

for k = 1 : 10
    A_{k} = rand(10,1);
end

I want it to create variables

A_1 = [.43,.234.,....]
A_2
A_3
...

But it doesnt. It only creates an variable A.

How do I feed the index into the loop to create individual variables?

jessica
  • 1,325
  • 2
  • 21
  • 35
  • It would be advised to keep it in a Map or something, rather then just polute the namespace. But if you realy want, you cat look at `eval` method to do what you want. – Marcin Nov 01 '16 at 02:45
  • This is not an ideal way to name your variables. It would be much better to collect the `A`s in an array and access (for a column vector) `A(:,1)` instead of `A_1`, `A(:,2)` instead of `A_2`, etc. One advantage of doing it this way is that if you ever need to iterate over all of the `A` variables, you can simply loop over the indices of the columns. – beaker Nov 01 '16 at 15:18
  • 2
    [Dynamic variable naming is bad](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170). As Wasi Ahmad pointed out, you're actually looking for an array. Please see the answer of mine which I linked and all references contained therein as to why this is a bad practise. – Adriaan Nov 01 '16 at 15:43

2 Answers2

1

What you are trying to do is creating an array which is a collection of variable.

A = zeros(10,1); % to initialize array
for k = 1 : 10
    A(k) = rand(10,1);
end

Now A is an array whose size is 10 and is containing 10 random values.

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • sorry I want the variable name to include the index number – jessica Nov 01 '16 at 02:46
  • A_1, A_2, A_3...etc not A(1) A(2) A(3) – jessica Nov 01 '16 at 02:47
  • access the data using A(1), A(2), ....., A(10). why you want to create 10 variables, it doesn't make sense. – Wasi Ahmad Nov 01 '16 at 02:47
  • because I am running a more complicated code. This is just to simplify the problem so other people can answer my question. – jessica Nov 01 '16 at 02:48
  • Even if you create bunch of variables, you can't refer a variable like A_{k} way. – Wasi Ahmad Nov 01 '16 at 02:48
  • Otherwise you can use map to store key, value pairs. Keys will be the variable names and the values will be what you want to store. – Wasi Ahmad Nov 01 '16 at 02:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127074/discussion-between-wasi-ahmad-and-jessica). – Wasi Ahmad Nov 01 '16 at 02:51
  • 2
    @jessica this actually is less simple. You'd need very stupid functions (like `eval`) to run this code, or copy every line a 100 times, or however many variables you'd need. I'd throw a code which used that many variables in the bin directly, as it's unreadable, bulky and slow. – Adriaan Nov 01 '16 at 15:46
-2

You can make variables dynamicly, using assignin, as follows:

for k = 1:10
    assignin('base', ['A_', num2str(k)], rand(10,1))
end

whos

  Name       Size            Bytes  Class     Attributes

  A_1       10x1                80  double              
  A_10      10x1                80  double              
  A_2       10x1                80  double              
  A_3       10x1                80  double              
  A_4       10x1                80  double              
  A_5       10x1                80  double              
  A_6       10x1                80  double              
  A_7       10x1                80  double              
  A_8       10x1                80  double              
  A_9       10x1                80  double              
  k          1x1                 8  double 
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • can I ask you another question? I want to run an m script many time, say 100 times. Each run, it would generate a vector and save it, run the script again, save it. What's the best way to do this? – jessica Nov 01 '16 at 03:00
  • @jessica No worries. If useful, accepting would be appricated. – Marcin Nov 01 '16 at 03:00
  • @jessica I replyied in your other question about the loop – Marcin Nov 01 '16 at 03:01
  • I am thinking of doing it through a function where I would feed the function the number of times it runs the scripts. The problem you solved creates the vector for that single run. – jessica Nov 01 '16 at 03:01
  • 5
    Please do not teach people [bad habits](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170). Dynamic variable naming is about the worst programming practise you can get in MATLAB, read that answer of mine as to why that is the case. – Adriaan Nov 01 '16 at 15:44
  • @Adriaan If you checked my earlier comments, you would see I recommeded against polluting global namespace, and use map or somemething else. But nevertheless my anwser is correct to OP question, and SO is about solving problems, not about telling ppl that their problem is bad and not giving them correct anwsers. There is no other solution to OP problem, unless you assume you know better what OP's real situation is. – Marcin Nov 01 '16 at 22:52
  • @Marcin the solution is most likely the presented cell array. Without the OP actually telling us what they want with these variables, I think it is safe to assume that the cell will be sufficient, since they asked for a way to automate the creation of a 1000 variables I assume they want to avoid writing everything out by hand, which the cell function does. Besides, which comments do you mean? I can't see any on this thread mentioning that this is a very bad habit to teach/use. – Adriaan Nov 01 '16 at 22:59
  • @Adriaan This is not code review nor programming SE webistes, where you can discass good practices etc. This is SO, when you privde solution to a problem presented by OP, rather then have discussions and making assumtions what OP wants or not in your opinion. You are free to make better anwser to OP's problem. There is none, unless you assume that OP does not know what she wants and what here situation is. p.s. My comment is at the top of OP question. – Marcin Nov 01 '16 at 23:06