How can I generalize this for loop?

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
------------------------

 Akzeptierte Antwort

Iain
Iain am 12 Jul. 2013

0 Stimmen

Whats wrong with?
t = cumsum(d)/total;

Weitere Antworten (2)

Thomas
Thomas am 12 Jul. 2013
Bearbeitet: Thomas am 12 Jul. 2013

0 Stimmen

a=1:9 % input
for ii=1:9
t(ii)=sum(a(1:ii))/sum(a);
end
t
Chandrasekhar
Chandrasekhar am 12 Jul. 2013

0 Stimmen

sum = 0;
for I = 1:r
sum = sum + d(I);
t(i) = sum/total;
end

Kategorien

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

Gefragt:

am 12 Jul. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by