Info

# Nested For loops- Pleeease HELP!!

1 view (last 30 days)
pramit Sood on 31 Mar 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi! Hope you guys are doing great! :) Well this is a very trivial problem I am facing but I dont know I am totally stuck.. Can't figure out where the problem is!!
Well I have two arrays (or matrices) out_M and out_R (just attached the text files with this question.. I have simply written this code:
*for j=1:1:11
val = out_R(j,1);
for k=1:1:107
if out_M(k,1) == val
out_M(k,2) = out_M(k,2)-out_M(1,2);
end
end
end*
And I guess you must have noticed what I try to do.. I just want to take the first column value from out_R and then enter the out_M matrix. Then I just compare the first column value of out_M with the first column value of out_R. Till these values match, I just want to subtract a fixed number from the second column values of out_M.
Thats it!! All the counters are incrementing absolutely fine!! but the result only appears for 1 cycle not the 107 cycles.. :( its getting frustrating..
Please guide me as to what is wrong!
Thank you so much!
cheers
Pramit

Titus Edelhofer on 31 Mar 2015
Hi Pramit,
if I take a look at out_M, then in the first entry you have zero. That's where you enter the "if" condition. All other values of out_M are different from all values of out_R, so you will never again enter into the if condition.
That said, I don't understand what you want to achieve, unfortunately, so I can't help more than the above. Try to formulate differently or in more detail.
Titus
pramit Sood on 31 Mar 2015
Ya you are right!! It would only occur for j=1 but the if statement should do the subtraction atleast 107 times when first column of out_M is '0'. It is doing the subtraction only once... :(
Regards
Pramit

Adam Wyatt on 31 Mar 2015
• Your code could easily be vectorized.
• never use i or j for indexing because these are used for sqrt(-1) and can easily cause bugs. I would use nR and nM so that it is clear that these indices refer to the loop over the R position and M position.
• You don't need to enter a step size of 1 (i.e. 1:11 is the same as 1:1:11)
Anyway, out_M(:, 1) always equals 0, so the contents of the case statement is not executed except when out_R==0 (i.e. the first j iteration). Also, M(1,2)==0, so that statement doesn't actually change any values!!
I would do the following (1 loop)
for nR=1:length(out_R)
&#09;test = (out_M(:, 1) == out_R(nR));
&#09;out_M(test, 2) = out_M(test, 2) - out_M(1, 2);
end
pramit Sood on 31 Mar 2015
Hi Adam! Thank you for your info notes.. :) Well the code also kind of works.. Thank you!! :)
But my original out_M matrix is what is attached here.. So as you see there are cycles of 0's, 1's ,2's and so on goes on uptil the end value of out_R that is 10.
Like I subtracted the first value of the second column i.e-out_M(1,2) from all the other values in out_M(:,2) for 0's cycle, I want to ask is it somehow possible if I can subtract the first value of 1's, 2's or 3's cycle from all the other values in the second column for that particular cycle..
I know it is confusing to explain.. for example- for 0's cycle - I subtract 55.752 for 1's cycle - I subtract 116.03 (from where the 1's cycle start) for 2's cycle - I subtract 176.294 (from where the 2's cycle start)
and so on..
I look forward to your suggestion..
Regards
Pramit

Andrei Bobrov on 31 Mar 2015
ii = ismember(out_M(:,1),out_R);
out_M(ii,2) = out_M(ii,2) - out_M(1,2);
?