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!