开发者

Change for loop index variable inside the loop

I need to change my loop variable inside the iteration as I have to access array elements in the loop which is changing w.r.t size inside the loop.

Here is my code snippet:

开发者_如何学运维que=[];
que=[2,3,4];
global len;
len=size(que,2)
x=4;
for i=1:len 
    if x<=10
    que(x)= 5;
    len=size(que,2)
    x=x+1;

    end
end
que

Array should print like:

2 3 4 5 5 5 5 5 5 5 

But it is printed like this:

2 3 4 5 5 5

In Visual C++ the array is calculated correctly and whole array of 10 elements is printed, which increases at run time.

How can I accomplish this in Matlab?


You should use a while loop instead of a for loop to do this:

que = [2 3 4];
x = 4;
while x <= 10
  que(x) = 5;
  x = x+1;
end

Or, you can avoid using loops altogether by vectorizing your code in one of the following ways:

que = [2 3 4];             %# Your initial vector
%# Option #1:
que = [que 5.*ones(1,7)];  %# Append seven fives to the end of que
%# Option #2:
que(4:10) = 5;             %# Expand que using indexing


Yes, you can use a (while) loop, but why? Learn the MATLAB way of doing things. Avoid this loop that has no need to exist.

For example, use this single line of code, that simply adds as many elements as it needs to add.

que = [que,repmat(5,1,10 - length(que))];

If you have some other way of determining how long your goal for this variable is, it will still be possible to create the array in one line using a similar scheme.

I might also ask why you are defining len to be a global variable in the code you posted?


Your return vector is exactly what you coded. The for-loop will run exactly 3 times - the length of que vector, this is why you get three 5s instead of seven. Changing len inside the loop will not help, since range for i variable is determined before the for-loop started and cannot be changed on run time. In other words, once the for-loop started, it forgets what is len, it just remembers that i has to be changed from 1 to 3.


Is initial x always equal length(que)+1?

What vector you expect if let's say x=5? 2 3 4 0 5 5 5 5 5 5? or 2 3 4 5 5 5 5 5 5?

or 2 3 4 5 5 5 5 5 5 5? (does not depend on x actually)

In the first case the best matlab-ish solution would be from @gnovice:

que = [2 3 4];  
x = 5;
y = 10;
val = 5;
que(x:y)=val;

In the second case another solution from @gnovice:

que = [2 3 4];
x = 5;
y = 10;
val = 5;
que = [que, val.*ones(1,y-x+1)];

or

que = [que, repmat(val, 1, y-x+1)];

In the third case, it's @woodchips' solution:

que = [que, repmat(val, 1, y - length(que))];

In your situation using loop (for or while) is really bad, because at every loop you increase the size of que vector with memory reallocation.


Programmatic loops in Matlab have awful performance. As everyone else has said, do it with matlab built-in functions and vectorize everything to the extent possible. If you really need looping constructs, perhaps C would be a better choice for you :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜