필터 지우기
필터 지우기

How can I generalize this for loop?

조회 수: 2 (최근 30일)
STamer
STamer 2013년 7월 12일
My question is easy. How can I generalize this for loop?
-----------------------
for i=1:r
t(1)=(d(1))/total
t(2)=(d(2)+d(1))/total
t(3)=(d(3)+d(2)+d(1))/total
t(4)=(d(4)+d(3)+d(2)+d(1))/total
.
.
.
end
------------------------

채택된 답변

Iain
Iain 2013년 7월 12일
Whats wrong with?
t = cumsum(d)/total;

추가 답변 (2개)

Thomas
Thomas 2013년 7월 12일
편집: Thomas 2013년 7월 12일
a=1:9 % input
for ii=1:9
t(ii)=sum(a(1:ii))/sum(a);
end
t

Chandrasekhar
Chandrasekhar 2013년 7월 12일
sum = 0;
for I = 1:r
sum = sum + d(I);
t(i) = sum/total;
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by