HOW to Overwrite Matrix Values in a loop, not Append!! ??

1 次查看(过去 30 天)
Hi guys! Hope you are doing great!
Well I run this particular code with the 4 matrices out_M, out_M_corr, out_R and out_R_corr.
(I have attached an EXCEL file - 'sample.xlsx' to show these matrices).
out_M_corr = out_M;
out_R_corr = out_R;
out_M_corr(:,3) = out_M_corr(:,3)*2; %these are the correction factors.....
out_M_corr(:,5) = out_M_corr(:,5)*5.3346;
for nR=1:size(out_R,1)
test = (out_M_corr(:,1) == out_R_corr(nR));
val = find(test,1,'first');
out_M_corr(test, 2) = out_M_corr(test, 2) - out_M_corr(val,2);
X(test,1) = out_M_corr(test,2);
Y(test,1) = out_M_corr(test,3);
poly = polyfit(X,Y,1);
out_R_corr(nR,4) = poly(1,1);
out_R_corr(nR,3) = poly(1,2);
end
The problem I am facing is in the loop. I want the matrices X & Y to always store new values and overwrite the values from previous iteration of the 'for' loop.
But to my surprise it is APPENDING, not OVERWRITING.. :( What can be the problem?
I would really appreciate your inputs!! PLEASE help!
Regards
Pramit

采纳的回答

Jan
Jan 2015-4-2
What about this:
X = out_M_corr(test,2);
Y = out_M_corr(test,3);
  1 个评论
pramit Sood
pramit Sood 2015-4-2
Hey Jan!!
Well I can't help but give thumbs up to you to teach me that thinking simple is the best idea!! It works! :D
Thank you so much!! N Happy Easter!!! Can I send you some chochlates?? :)
Regards
Pramit

请先登录,再进行评论。

更多回答(1 个)

Roger Stafford
Roger Stafford 2015-4-2
If you want X and Y to be completely overwritten on each trip through the for-loop, you will have to make some provision for their values for the elements for which 'test' is false. Otherwise these "false test" elements, though initially zero, will thereafter be what they were on the previous pass. Probably you should do something like this:
X = zeros(size(out_M_corr,1));
X(test,1) = out_M_corr(test,2);
and similarly for Y.
  2 个评论
pramit Sood
pramit Sood 2015-4-2
Hi! thank you for your reply.. :) Unfortunately that does not help.. as I had tried doing it earlier.. It gives '0's for the old values.. what I want is the old values to be completely deleted and only new values from the current iteration to remain.. :)
Regards
Pramit
pramit Sood
pramit Sood 2015-4-2
Hi Roger!
Thank you for the initiative to help me!!
Joyful easter holidays to you!!
Regards
Pramit

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Holidays / Seasons 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by