6

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?

bluish
  • 26,356
  • 27
  • 122
  • 180
shawana
  • 61
  • 1
  • 1
  • 2

4 Answers4

10

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
  • 125,304
  • 15
  • 256
  • 359
  • 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 May 14 '10 at 06:37
  • 1
    @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 May 14 '10 at 13:53
3

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?

0

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
  • 19,098
  • 13
  • 68
  • 99
0

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 :)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
vicatcu
  • 5,407
  • 7
  • 41
  • 65