adding numbers in an M file

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

답변 (2개)

Matt Fig
Matt Fig 2011년 5월 12일

0 개 추천

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

Andrew Newell
Andrew Newell 2011년 5월 12일
But loops are all the rage these days!
Matt Fig
Matt Fig 2011년 5월 12일
Haha!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

제품

태그

질문:

2011년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by