How can I calculate the revenue in a FOR loop?

조회 수: 1 (최근 30일)
Fábio Sousa
Fábio Sousa 2021년 2월 20일
댓글: Rik 2021년 2월 21일
for s=1:4 %number of weeks
n=input('How many packages?')
for k=1:n
.. code..
end
end
So I have this loop and I want to calculate the revenue at the end of each week and the total revenue at the end of the 4 weeks. how can I do it?
I have multiple IFs inside a FOR loop inside of another FOR loop. So if the user chooses one option it will give one revenue and then it will go back and loop 2 more times. This process will continue for 4 times. So basically I need to sum the revenue 3 times each 4 weeks. Its a bit confusing.

답변 (1개)

Rik
Rik 2021년 2월 20일
Store the revenue in a vector you index with s. Then use sum to calculate the total.
  댓글 수: 2
Fábio Sousa
Fábio Sousa 2021년 2월 20일
How? Can you give me an example? I am newbie at Matlab.
Rik
Rik 2021년 2월 21일
You should consider reviewing the material that was probably given during the first lesson in your course. Since this sounds like homework I'm not going to give a complete solution.
Looking at your edited question it seems you should store the revenue in a 2D array and index with s and k, although that would require the user to enter the same number of packages every iteration of s.

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

카테고리

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