adding numbers in an M file
Mostrar comentarios más antiguos
Here is my code. At the end I want it to add up all of the values.
a = input('Enter number of days you would like to know the total of');
f(1) = .01;
for i = 2:a
f(i) = (2*f(i-1));
end
can I just add sum(sum(2:a)) before the "end"?
Respuestas (2)
Andrew Newell
el 12 de Mayo de 2011
sum(f)
(after the loop)
Matt Fig
el 12 de Mayo de 2011
A loopless version...
a = input('Enter number of days you would like to know the total of: ');
S = sum(.01.*2.^(0:(a-1)))
If you need f for other calculations:
f = .01.*2.^(0:(a-1));
S = sum(f)
2 comentarios
Andrew Newell
el 12 de Mayo de 2011
But loops are all the rage these days!
Matt Fig
el 12 de Mayo de 2011
Haha!
Categorías
Más información sobre App Building en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!