views:

384

answers:

4

hi every one. i need to change my loop variable inside the iterationa as ihave to acces array elements in the loop which is changing w.r.t size inside the loop. ashort piece of code is that:

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 in such a way: 2 3 4 5 5 5.

how shuould it be accomplished in matlab? in visual c++ it happen correctly and print whole array of 10 elements which increases at run time plz reply me if have any ideaabout this as i m new into matlab

+4  A: 

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
gnovice
but i wud not know in advance that hows much the size of array wud increases. it was just n example. actually the size of array increases dynamically on the basis of condition. som time it increases bt 2 elements n some time by 3, n some time by non.
shawana
@shawana: Even so, you would still want to use a while loop or one of the vectorized solutions I gave. For loops are designed to loop a fixed number of times, which is known at the time the loop begins. If the number of times you have to loop is based on some condition, you should use a while loop. However, I think your situation would be best handled by using vectorized code as opposed to loops.
gnovice
+1  A: 

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?

woodchips
A: 

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.

yuk
A: 

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

vicatcu