Output standard deviation and Mean

2 visualizzazioni (ultimi 30 giorni)
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 Commento
August Hardie
August Hardie il 2 Giu 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.

Accedi per commentare.

Risposta accettata

David Hill
David Hill il 2 Giu 2020
Modificato: David Hill il 2 Giu 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

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by