5-9.m

来自「《MATLAB 7.0编程基础》第5章 (源码实例)主要讲解MATLAB的编程基」· M 代码 · 共 27 行

M
27
字号
%stats_1.m
%purpose: to calculate mean and the standard deviation of 
%an input data set containing an arbitrary number
%of input values.
%Define variable
%n   --The number of input samples
%std_dev --The standard deviation of the input samples
%sum_x  --The sum of the input values
%sum_1x --The sum of the squares of the input values 
%x    --An input data value
%xbar  --The average of the input samples
%Initialize sums.
n=0;sum_x=0;sum_x1=0;
x=input('Enter first value:');
while x>=0
    n=n+1;
    sum_x=sum_x+x;
    sum_x1=sum_x1+x^2;
    x=input('Enter next value:');
end
%calculate the mean and standard deviation
x_bar=sum_x/n;
std_dev=sqrt((n*sum_x1-sum_x^2)/(n*(n-1)));
fprintf('The mean of this data set is :%f\n',x_bar);
fprintf('The standard deviation is :%f\n',std_dev);
fprintf('The number of  data points  is :%f\n',n);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?