Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Summing from control variable in loop to end of loop / from beginning
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a matrix 1738 * 2 and I want to loop through the first column. 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. Thanks for every advice
댓글 수: 0
답변 (1개)
Weird Rando
2016년 5월 9일
편집: Weird Rando
2016년 5월 9일
Don't necessary need a loop you could do something like this
startValue = 2;
control = 10;
a=[1:1:1738; 11:1:1748]';
a1 = sum(a(:,1))
a2 = sum(a(startValue:(control - 1),1))
댓글 수: 2
Weird Rando
2016년 5월 10일
편집: Weird Rando
2016년 5월 11일
I think you when wrong is when you initialise the for loop statement. It gives the COG_Ton values. So if COG_Ton(1,1) = 1024,
tonnes(1024) = sum(COG_Ton(1:1024-1,2)).
If that number exceed the length of COG_Ton you will incur an error.
Here my solution.
nloop = size(COG_Ton,1)-1;
for ik = 1:nloop
tonnes(ik) = sum (COG_Ton (1:ik, 2));
tonnes2(ik) = sum(COG_Ton (ik+1:end,2));
end
However if you did that, you would probably need to amend your code if there were some other code inside that for loop that uses ik and replace that as COG_Ton(ik,1).
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!