Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Nested For loops- Pleeease HELP!!
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (3)
Titus Edelhofer
il 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
3 Commenti
Adam Wyatt
il 31 Mar 2015
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
Adam Wyatt
il 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)
	test = (out_M(:, 1) == out_R(nR));
	out_M(test, 2) = out_M(test, 2) - out_M(1, 2);
end
1 Commento
Andrei Bobrov
il 31 Mar 2015
ii = ismember(out_M(:,1),out_R);
out_M(ii,2) = out_M(ii,2) - out_M(1,2);
?
0 Commenti
Questa domanda è chiusa.
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!