Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Sum variables from three in three, after summing compute an equation and organize all data into a new column?
1 回表示 (過去 30 日間)
古いコメントを表示
How can I sum variables from three in three and put these same sums in another column? I thought in splitting in two "for" conditions as shown below.
x=data(:,1);
i=1;
j=1;
for i=1:length(data)
for j=1:3
x(i)=1;
x(i+1)=2^2;
x(i+2)=3^3;
h=x(i)+x(i+1)+x(i+2);
i=i+1;
end
j=j+3;
end
Can anyone help me, please? Thank you.
1 件のコメント
回答 (2 件)
Steven Lord
2019 年 1 月 27 日
x=data(:,1);
i=1;
j=1;
for i=1:length(data)
for j=1:3
x(i)=1;
x(i+1)=2^2;
x(i+2)=3^3;
h=x(i)+x(i+1)+x(i+2);
i=i+1;
end
j=j+3;
end
The line "j=j+3;" at the end of the body of your "for i" loop does increment j ... for the remainder of that iteration over i. As soon as MATLAB moves to the next iteration of the "for i" loop, it starts a loop that assigns 1, 2, and 3 in turn to j. This throws away the value that was previously in j (which was not actually used in the for loop over j.)
Perhaps what you want is to have i jump by steps of 3? Look at the documentation for the colon function or the : operator for examples that will show you how to do that.
1 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!