Sub-Function help!

조회 수: 2 (최근 30일)
Jay
Jay 2016년 3월 13일
댓글: MHN 2016년 3월 14일
I have a homework that I don't understand. This what it says
You need to make a function of [A,B] = mystatistic(Scores).
A = (average of Test 1 ) * (standard deviation of Test 3)
B = (median value of Test2) / (average of Test2)
(1) Use of sub-function
(2) Use of inline function for the sub function
I am not sure how to do [A,B] = mystatistic(Scores). Also I know what is a sub-function but I don't get it here.
Your help is highly appreciated.

답변 (1개)

MHN
MHN 2016년 3월 13일
편집: MHN 2016년 3월 13일
Based on the question it seems "Scores" is a N*3 matrix (N shows number of students). You should write a function that outputs A and B. So your main function will look like
function [A,B] = mystatistic(Scores)
%Calculation
end
To test your program use the following Score as an example (N=10)
Scores = randi(100,10,3) %let assume the exam is out of 100
Now, try the code yourself and if you have question let us know. We are not going to solve your assignment! ;)
  댓글 수: 2
Jay
Jay 2016년 3월 13일
Thank you for your answer. Here what I got and I don't know what I am doing. Please guide me
function [A,B] = mystatistic(Scores)
fid = fopen('TestScoresKim.txt','r'); % open file
headings = fgetl(fid); fgetl(fid); Scores = fscanf(fid,'%f'); Scores = reshape(Scores,[4 20])'; display(Scores);
Test1 = Scores(:,2); Test2 = Scores(:,3); Test3 = Scores(:,4);
A = ((mean(Test1)) * (std(Test3))) B = (median(Test2)) * (mean(Test2))
end
MHN
MHN 2016년 3월 14일
function [A,B] = mystatistic(Scores)
fid = fopen('TestScoresKim.txt','r'); % open file
headings = fgetl(fid); % read the first line
fgetl(fid); % ignore the second line
Scores = fscanf(fid,'%f'); % read the numbers in the text file
Scores = reshape(Scores,[4 20])'; % put those numbers in a 20*4 matrix
display(Scores);
Test1 = Scores(:,2); %2nd column shows test1
Test2 = Scores(:,3); %3rd column shows test2
Test3 = Scores(:,4); %4th column shows test3
A = ((mean(Test1)) * (std(Test3))) % compute A
B = (median(Test2)) * (mean(Test2)) % compute B
end
But you should also used subfunction, for example you can write the code for mean by yourself (just a loop which sum up the elements of a vector divided by size).

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

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by