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!