Summing from control variable in loop to end of loop and from beginning of matrix to control variable

1 Ansicht (letzte 30 Tage)
As a part of a bigger script, I have a matrix 1738 * 2 (1738 rows - 2 columns) and I want to loop through the first column (so 1738 times). Dependent on the iteration of the loop I would like to sum up the the second column from the start value to the (control variable - 1) and from the control variable to the end of the second column. This is how i try to start with the loop through the first column - COG_Ton is the 1738 x 2 Matrix (although this is also variable on the input data).
for ik = COG_Ton (:,1)
tonnes(ik) = sum (COG_Ton (1:ik-1, 2))
tonnes2(ik) = sum(COG_Ton (ik:end,2))
end;
Unfortunately, I dont know how to write this in Matlab. Advice is warmly welcome.
  1 Kommentar
Ced
Ced am 9 Mai 2016
Bearbeitet: Ced am 9 Mai 2016
Hi
ik is a loop variable, i.e. the index you are looping through. Unless COG_Ton(:,1) happens to be 1:1738, this is not what you want.
Have a loop at the documentation for for loops and if statements

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by