-1

I would like to name variable (type double) in the following way:

k0 = D(1,1);
k1 = D(2,2);
k2 = D(3,3);
k3 = D(4,4);
k4 = D(5,5);
k5 = D(6,6);
k6 = D(7,7);
k7 = D(8,8);
...

up to k99 automatically using for loop. So I see that I should use array or cell instead of double variable using eval as it is slow. But if I should use array or cell instead of double variable, I have to start at k{1} or k(1), which loses the meaning as I want exactly that k0 refers to D(1,1), i.e. the number in my variable is 1 less. How do I create meaningful cell name like k{0}?

Also, say I have an array A. There are also some times i need meaningful variable name, such as

c111 = A(1)*A(1)*A(1)
c222 = A(2)*A(2)*A(2)
c333 = A(3)*A(3)*A(3)

How can I create c{111} efficiently using for loop?

Ka Wa Yip
  • 2,546
  • 3
  • 22
  • 35
  • Wait. You want to go against your language's indexing conventions, *and* make the correspondence between index and value less direct? Why would you prefer `k(1) = D(2, 2);` over `k(1) = D(1, 1);`? – user2357112 Aug 03 '16 at 04:44
  • yes... this is just one example. I also have some variable using negative index. – Ka Wa Yip Aug 03 '16 at 04:46
  • Variables are for the programmers to read the code. In case you want to use the names outside the code you make an array such as `kNames = {'..., k-2, k-1, k0, k1, k2, ...'}`. you do not need to automatically create variables in a loop and this makes you lose track of the code. – patrik Aug 03 '16 at 06:07
  • I need to use cell or array as it is faster, not eval like http://stackoverflow.com/questions/17554623/i-would-like-to-create-a-loop-that-creates-a-different-variable-name-after-each – Ka Wa Yip Aug 03 '16 at 06:31
  • But if I use cell or array, I lost the meanings. – Ka Wa Yip Aug 03 '16 at 06:31
  • @kww For the negative values there is no way to solve this problem. Apart from this, I would do as one of the answer and use a struct if I decided that I need this. Apart from this, I have never seen a need to explicitly spawn hundreds of variables. In case you can explain why this is needed, we may be able to give better help how to do your design. After all this was what you asked for. There are already 2 answers to the question, so if these are not helping I would say that you need to give us a better explanation of the problem (by pressing the edit button) – patrik Aug 03 '16 at 08:56

2 Answers2

1

Answer to 1st Question:-

D=randn(100);    % A matrix of random elements of size 8x8
for m=0:99
    assignin('base', ['k' num2str(m)], D(m+1,m+1))
end

Answer to 2nd Question:-

A=randn(1,3);    % An array of 3 random elements 
for n=1:3
    assignin('base', ['c' num2str(111*n)], A(n)^3)
end 

Comments:-
You've stated that you need variables like k0,k1,k2,... and c111,c222,c333 but you're asking how to create k{0}, k{1},k{2},... and c{111},c{222},c{333}. As far as your need is concerned, I have given answer to it. Regarding the latter, k{0} is never possible and c{111},c{222},c{333},... don't make good sense without using any of the first 0:100 values and then 112:221 values and so on. Although you can do it using:

A=rand(1,3);       % An array of 3 random elements 
c{333} = 0 ;       % Pre-allocation
for p=1:3          % Since you want to use a 'for loop'
  c{111*p} = A(p)^3;
end

And regarding the requirement that you made in the comment in these words "I also have some variable using negative index", you can never have variables in the negative index. If you mean you want to create variables with names like k-1, k-2,... etc, it is not possible. An alternate way is to use k_1, k_2,... etc but then as you said in the question "k0 refers to D(1,1), i.e. the number in my variable is 1 less". It means k_1 will refer to D(0,0) and so on which is again an invalid thing for MATLAB.

Recommendation:-
You really need to modify your code.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
1

Use structures:

D = rand(21);
c = 1;
for k = -10:10
    if k<0
        s.(['k_' num2str(abs(k))]) = D(c,c);
    else
        s.(['k' num2str(k)]) = D(c,c);
    end
    c = c+1;
end

This will give you a structure like:

s = 

     k_10: 0.51785
     k_9: 0.90121
     k_8: 0.40746
     k_7: 0.092989
     .
     .
     k_1: 0.75522
     k0: 0.55257
     k1: 0.28708
     .
     .
     k9: 0.94182
     k10: 0.2124

and don't use eval...

EBH
  • 10,350
  • 3
  • 34
  • 59
  • This will cause troubles with the negative k I think. Anyway, this is what I would prefer otherwise, if I for some reason needed lots and lots of variables. – patrik Aug 03 '16 at 08:47
  • 1
    @patrik I did some editing, but you can't have a variable / field named with `-` anyway, so it is silly to persist on this nomenclature. Whatever reason the OP had to declare so many variables, I think that's a better way of doing this. – EBH Aug 03 '16 at 10:13
  • Right I totally agree there. It is often better way. My guess is that the OP really requests some kind of map. I do not think that the data structure requested actually exist (in any language). – patrik Aug 03 '16 at 10:29
  • `k0` should be equal to `D(1,1)` – Sardar Usama Aug 03 '16 at 11:30
  • So what is `k-1`? `D(0,0)`?! unless the OP will give a better explanation of the problem I have no other assumption to make. – EBH Aug 03 '16 at 11:34