开发者

change name of a variable inside a loop [duplicate]

This question already has answers here: Closed 11 years ago.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜