Output standard deviation and Mean

2 views (last 30 days)
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 Comment
August Hardie
August Hardie on 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.

Sign in to comment.

Accepted Answer

David Hill
David Hill on 2 Jun 2020
Edited: David Hill on 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

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!