Info
This question is closed. Reopen it to edit or answer.
Sum variables from three in three, after summing compute an equation and organize all data into a new column?
1 view (last 30 days)
Show older comments
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 Comment
Answers (2)
Steven Lord
on 27 Jan 2019
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 Comment
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!