**Info**

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

1 view (last 30 days)

Show older comments

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

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)

	test = (out_M(:, 1) == out_R(nR));

	out_M(test, 2) = out_M(test, 2) - out_M(1, 2);

end

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);

?

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

Start Hunting!