function with two outputs

조회 수: 2 (최근 30일)
Kendall Donahoe
Kendall Donahoe 2020년 4월 28일
답변: Steven Lord 2020년 4월 29일
I'm trying to create a function that finds the mean and median of testscores (x). I am then supposed to call a specific input i.e. x=[ 80, 62, 97, 73, 86]. and then use the outputs to find the difference between the mean and the median outside of the function. this is what I have so far:
function [m,d]=testscore(x)
n=length(x);
m= sum(x)/n;
d= median(x);
end
  댓글 수: 2
Toder
Toder 2020년 4월 28일
This looks fine. What is your question?
Geoff Hayes
Geoff Hayes 2020년 4월 29일
Kendall - you can also use the mean function.

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

답변 (1개)

Steven Lord
Steven Lord 2020년 4월 29일
I'm taking a guess at the problem you're experiencing.
In order to obtain one or more outputs from a function, two conditions must be satisfied.
  1. The function must be defined such that it returns one or more outputs.
  2. The function must be called with one or more outputs, and you will receive as many outputs as you request.
Your function satisfies the first of those conditions. Its definition states that it returns two variables, m and d.
If you call the function with fewer outputs than it defines, MATLAB will return the first however many output arguments you requested. So if you asked:
m1 = testscore(1:10)
you're only going to receive the first output in testscore definition, m. If you asked for both you'll get both.
[m2, d2] = testscore1(1:10)
There's a special case for 0 outputs: see the documentation for the ans function.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by