Effacer les filtres
Effacer les filtres

Output standard deviation and Mean

2 vues (au cours des 30 derniers jours)
Christopher Clarke
Christopher Clarke le 2 Juin 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 commentaire
August Hardie
August Hardie le 2 Juin 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.

Connectez-vous pour commenter.

Réponse acceptée

David Hill
David Hill le 2 Juin 2020
Modifié(e) : David Hill le 2 Juin 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

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by