From a For Loop to a While Loop

Hello. Beginner here on MATLAB.

How do I change this for loop I made into a while loop and get the same result?

1
2
3
4
5
6
7
8
9
10
11
12
13
x=6*rand(1,10)+2

for i=1:10
    if i<5
        x(i)=-1;
    elseif i>=5 && i<8
        x(i)=1;
    else
        x(i)=3;
    end
end

x
Last edited on
this isn't a matlab forum.

but this in c++ would just be

1
2
3
4
5
6
7
8
9
10
i = 1;
while(i != 10)
{
    x[i] = 3; //default
    if(i<5)
         x[i] = -1;
    else if(i < 8) // the check on 5 is redundant, its in the else on the same check
      x[i] = 1;    
   i++;
}


it would look better as a do-while.
remember that arrays in c++ are from 0, not 1.
Last edited on
Oh, ok. Sorry about that. Thanks!
Topic archived. No new replies allowed.