Output standard deviation and Mean

2 Ansichten (letzte 30 Tage)
Christopher Clarke
Christopher Clarke am 2 Jun. 2020
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 Kommentar
August Hardie
August Hardie am 2 Jun. 2020
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

David Hill
David Hill am 2 Jun. 2020
Bearbeitet: David Hill am 2 Jun. 2020
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

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by