필터 지우기
필터 지우기

Output standard deviation and Mean

조회 수: 2 (최근 30일)
Christopher Clarke
Christopher Clarke 2020년 6월 2일
댓글: Christopher Clarke 2020년 6월 2일
When i run my programme, how do i only output the fprintf values?
function [VSD,M] = mean_stdev(A)
A=input('N numbers:')
% This function calculates the mean and standard deviation without
% using in-built functions
som=0;
for i=1:length(A)
som=som+A(i);
end
M=som/length(A) %the mean
fprintf('Mean is: %f\n', M)
moy=0;
for i=1:length(A)
moy = (A(i)- M)^2;
moy = (moy)/length(A);
end
VSD=sqrt(moy/length(A)) %Varaince
fprintf('stadard dev: %f\n',VSD)
end
The output
>> mean_stdev
N numbers:
[5 6 7 8]
A =
5 6 7 8
M =
6.5000
Mean is: 6.500000
VSD =
0.3750
stadard dev: 0.375000
ans =
0.3750
>>
  댓글 수: 1
August Hardie
August Hardie 2020년 6월 2일
Add semicolons at the end of statements to suppress output.
A=input('N numbers:') ;
M=som/length(A) ; %the mean
VSD=sqrt(moy/length(A)) ; %Varaince
Lastly, to suppress the 'ans' output, add a semicolon to the end of your function ' mean_stdev( ) ; ' whenever calling it.

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

채택된 답변

David Hill
David Hill 2020년 6월 2일
편집: David Hill 2020년 6월 2일
Function call. I would do the printing after the function call returns. But, this should work for you.
[s,m]=mean_stdev();
After function call returns.
A=input('N numbers:');
[VSD,M]=mean_stdev(A);
fprintf('Mean is: %f\n', M);
fprintf('stadard dev: %f\n',VSD);
function [VSD,M] = mean_stdev(A)
som=0;
for i=1:length(A)
som=som+A(i);
end
M=som/length(A); %the mean
moy=0;
for i=1:length(A)
moy = (A(i)- M)^2;
moy = (moy)/length(A);
end
VSD=sqrt(moy/length(A)); %Varaince
end

추가 답변 (0개)

카테고리

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