change name of a variable inside a loop [duplicate]
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)
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
精彩评论