Info

This question is closed. Reopen it to edit or answer.

loops misunderstanding code statement

1 view (last 30 days)
Rick
Rick on 20 Jun 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
num = 0;
L= randperm(10);
for k = L
while L(k)<4
num = num + 1;
L(k) = L(k)+1;
end
end
I am having some confusion about what this code is doing. I can see L is an 1 x 10 array. Then the for k = L says to repeat L amount of times (how can you repeat something an array amount of times??). Then while says repeat indefinite for the elements of k within L < 4 (what the heck is this???). If L = k, L(k). Things are basically muddy at the for statement and from there not discernible to me as of yet.
  1 Comment
Roger Stafford
Roger Stafford on 20 Jun 2014
Why don't you try executing that code? It makes perfectly good sense. Try running it under debug mode and watch it take each step. In particular you will find that 'num' will have climbed up to 6 when execution stops. See if you can see why. I guarantee you will learn something about matlab if you do.

Answers (1)

virinchi chilukuri
virinchi chilukuri on 20 Jun 2014
Edited: virinchi chilukuri on 23 Jun 2014
This is somewhat interesting question.First you need to know about the randpem function. Then you can automatically understand the rest of the code. Here is the link: http://www.mathworks.in/help/matlab/ref/randperm.html.
From this you can understand that randperm(10) means that numbers from the set [1,2,3....,10] are randomely permuted and returned to L. So lets say we got this, L = [4 5 6 1 2 10 9 7 8 3] (I have run the loop and this is the value I got in one of those runs). So for k=L means first it will take the value in the first index which is 4,go to the inner loop and execute it. Next it will take the value in the second index, which is 5,go to the inner loop and execute it.Next it will take the value in the third index, which is 6,go to the inner loop and execute it and so on. Now look at the value of the first index:k = 4 and it goes to inner loop which executes this condition: while L(k)<4, which in this case translates to while L(4)<4, which is true since 1<4. So it executes the while loop until the condition fails which is when L(k) becomes 4. After that L(4) becomes 4 and num = 3(since L(4) starts from 1 and then goes to 2 and then goes to 3 and then to 4 and then the while loop fails). So next k becomes equal to the second index which is 5. Now it goes to the inner loop, checks if L(5)<4, which is true since 2<4. So the while loop executes 2 times until L(k) becomes 4 and num becomes 5 and then the while loop fails. Then k becomes 6 and L(k)= L(6)=10. But L(6)<4 is false. Hence while loop is not executed. Next k=fourth index=1.L(1)=4. But L(4)<4 is false. So,next k= fifth index=2.L(k)=L(2)=5<4 is false. So,next k= sixth index=10.L(k)=L(10)=3<4 is true. So,while loop executes one time until L(k)=4,num=6. So,next k= seventh inde=9.L(k)=L(9)=8<4 is false. So,next k= eighth index=7.L(k)=L(7)=9<4 is false. So,next k= ninth index=8.L(k)=L(8)=7<4 is false. So,next k= tenth index=3.L(k)=L(3)=6<4 is false. Now num will always be equal to 6(3+2+1).The reason I gave such a lengthy answer is below:Suppose in the while loop L(k)<6 is the condition instead of L(k)<4,num will always turn out to be 15(5+4+3+2+1).Suppose in the while loop L(k)<8 is the condition instead of L(k)<4,num will always turn out to be 36(8+7+6+5+4+3+2+1). Think about it. Also,when randperm(n) is used instead of randperm(10) and L(k)<m where m<n then num will always turn out to be 1+2+3+....+(m-1) = m*(m-1)/2. Test this using your code and changing the variables as suggested. Hope this answer helps.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!