-7

Possible Duplicate:
How to concatenate a number to a variable name in MATLAB?

It must be easy but I just cannot find it in help! I am operating with a vector x for 10 loops (for example) and at the end I want to concatenate all the results in a matrix 10by10. In order to do that I have to name them x1,x2,x3 etc. how can I do this?

Edit: A portion of my code thus far (copied from comments):

x = [0,0,0,1,0,0,1,0];
for k = 1:50 
    if x(1,8) ==1 && x(1,1)==1 && x(1,2)==1
        x(1,1)=0;
    elseif x(1,8) ==1 && x(1,1)==1 && x(1,2)==0
        x(1,1)=0;
    elseif x(1,8) ==1 && x(1,1)==0 && x(1,2)==1
        x(1,1)=0;
    elseif x(1,8) ==1 && x(1,1)==0 && x(1,2)==0
        x(1,1)=1;
    elseif x(1,8) ==0 && x(1,1)==1 && x(1,2)==1
        x(1,1)=1;
    elseif x(1,8) ==0 && x(1,1)==1 && x(1,2)==0
        x(1,1)=1;
    elseif x(1,8) ==0 && x(1,1)==0 && x(1,2)==1
        x(1,1)=1; 
end

...etc...

disp(x)
Community
  • 1
  • 1
kojikurac
  • 205
  • 2
  • 4
  • 11

1 Answers1

3

You should preallocate a matrix before your loop, and in the loop you just insert the vectors directly in the columns (or rows). Like:

A= zeros(10, 10);
for k in 1: 10
    A(:, k)= %# result of your processing
end
eat
  • 7,440
  • 1
  • 19
  • 27
  • no, that totally isn't what I meant - I want to know not just for this case but for the future how I can add a value of k to my variable x so I get x1 =... x2 =... x3=... etc. – kojikurac Jul 09 '11 at 09:44
  • 1
    That's how I interpreted your question. You need to give more details what you are aiming for. Show some code to clarify your question. Why you need to create new variables inside a loop? Surely it's possible, but is it really the way to solve your case, I doubt. Thanks – eat Jul 09 '11 at 09:52
  • I am a composer not a programmer so I just want to know some logical operations I learned on MAXMSP and similar programs, but I find it hard to find my way through here. I tried to send u my code, but it's too long... and I tried what you suggested and it does not work - reports error :( – kojikurac Jul 09 '11 at 09:57
  • Surely you can extract the relevant part of your code, so it can be shown here. My example demonstrates the concept, which you should apply on your case. You need to show how you applied it. Please note how much negative feedback you have got already. Please try to turn that trend. Thanks – eat Jul 09 '11 at 10:04
  • ups - how do I post my code??? – kojikurac Jul 09 '11 at 10:12
  • Post the code in your question, and identify clearly that you have updated it. Remove also pointless comment. Thanks – eat Jul 09 '11 at 10:17
  • @kojikurac let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1281/discussion-between-eat-and-kojikurac) – eat Jul 09 '11 at 10:17