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
0 个评论
采纳的回答
更多回答(1 个)
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.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!