Using a function to calculate and average

조회 수: 2 (최근 30일)
Josh Dray
Josh Dray 2018년 8월 3일
댓글: Adam Danz 2018년 8월 3일
I'm attempting to use a function to take in 12 inputs from the user and calculate an average of the inputs, the output should then be to output a 'pass' or 'fail' comment depending on the average being above/below a certain value.
The main issue I'm having is doing this within a function as writing a program for this in a script would not be a problem, however, I think my base understanding of how a function operates is not quite there despite reading up on all I can find about utilizing functions to do something like this.
Not looking for a specific solution but any aid in helping me understand how to solve this would be ideal, thanks!
  댓글 수: 3
Adam
Adam 2018년 8월 3일
Just put your 12 inputs into an array and pass them to the function as an argument:
function result = calculate( inputs )
...
end
and the body of your function can be just as you would do it in a script. Dealing with 12 individual arguments would be silly so putting them in an array is much easier and makes the averaging a simple one line command.
Adam Danz
Adam Danz 2018년 8월 3일
Reading through this might help and focus on the examples of how Matlab's 'mean' function works.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by