📄 stats.m
字号:
% Script file stats.m
%
% Purpose:
% To calculate mean and the standard deviation of
% an input data set containing and arbitrary number
% of input values.
%
% Define variables:
% n The number of input samples
% std_dev The standard devation of the input samples
% sum1 The sum of the input values
% sum2 The sum of the squares of the input values
% x input data value
% xvar The average of the input samples
% Initalize variables
n=0;sum1=0;sum2=0;
%Read the input values
x=input('Enter the first value: ');
%创建求解数组元素和的循环结构
while x>=0
n=n+1;
sum1=sum1+x;
sum2=sum2+x^2;
% 读入原始数据
x=input('Enter next value: ');
end
%计算平均值和标准方差
xvar=sum1/n;
std_dev=sqrt((n*sum2-sum1^2)/(n*(n-1)));
%在命令窗口显示结果
fprintf('The mean of this data set is: %f\n',xvar);
fprintf('The standard deviation is: %f\n',std_dev);
fprintf('The number of data is: %d\n',n);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -