필터 지우기
필터 지우기

For loop stops after one step

조회 수: 4 (최근 30일)
Avik Mahata
Avik Mahata 2021년 8월 25일
댓글: Avik Mahata 2021년 8월 25일
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

채택된 답변

Steven Lord
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
x = 100
y
y = 1×10
1 4 9 16 25 36 49 64 81 100
  댓글 수: 1
Avik Mahata
Avik Mahata 2021년 8월 25일
Well y(k) worked. I didn't realized I am not indexing each of the rows for it save the results in a matrix. Thanks for taking time to respond.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by