Info

此问题已关闭。 请重新打开它进行编辑或回答。

Nested For loops- Pleeease HELP!!

2 次查看(过去 30 天)
pramit Sood
pramit Sood 2015-3-31
关闭: MATLAB Answer Bot 2021-8-20
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

回答(3 个)

Titus Edelhofer
Titus Edelhofer 2015-3-31
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
  3 个评论
Adam Wyatt
Adam Wyatt 2015-3-31
Your problem still does not make sense - M(:, 1) is always zero, so the case statement will only occur for j=1, and for all M(:, 1).
The statement "// Till the first column values are zero in out_M, I want to do this subtraction" doesn't make sense (neglecting the incorrect comment symbol). Do you want to do the subtraction until M(k, 1)~=val and then move on to the next j? then use if out_M(k,1)~=val; continue; else; out_M(k, 2) = ...; end
pramit Sood
pramit Sood 2015-3-31
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
Adam Wyatt 2015-3-31
  • 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)
	test = (out_M(:, 1) == out_R(nR));
	out_M(test, 2) = out_M(test, 2) - out_M(1, 2);
end
  1 个评论
pramit Sood
pramit Sood 2015-3-31
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
Andrei Bobrov 2015-3-31
ii = ismember(out_M(:,1),out_R);
out_M(ii,2) = out_M(ii,2) - out_M(1,2);
?

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by