For loop stops after one step
4 次查看(过去 30 天)
显示 更早的评论
Why does the below for loop stops after one step? L is a up to 1000. So I am expecting to get a matrix with 1000 rows. Thanks in advance for any suggestions.
for i=1:L
X= ((data_P_H2O(1,3).*data_P_NaF(i+1,3))-(data_P_NaF(1,3).*data_P_H2O(i+1,3)))*0.5 ;
Y = ((data_P_H2O(1,4).*data_P_NaF(i+1,4))-(data_P_NaF(1,4).*data_P_H2O(i+1,4)))*0.5 ;
Z = ((data_P_H2O(1,5).*data_P_NaF(i+1,5))-(data_P_NaF(1,5).*data_P_H2O(i+1,5)))*0.5 ;
PhiIW= X + Y + Z;
end
0 个评论
采纳的回答
Steven Lord
2021-8-25
What makes you believe the loop stops after one step? Were you expecting PhilW to contain the sum of X, Y, and Z for each element of i? As written at each iteration you're overwriting the previous value of PhilW with the new value.
Compare:
for k = 1:10
x = k.^2; % Overwrite
y(k) = k.^2; % Append
end
x
y
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!