⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stats2.m

📁 matlab宝典,电子工业出版社,包含该书的源码4-6章
💻 M
字号:
% Script file stats2.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
sum1=0;sum2=0;

% 输入排序数字的个数
n=input('Enter the number of points: ');
% Check to see if we have enough input data.
if n<2
    disp('At least 2 values must be entered.');
else
    % 创建计算总和和平方和的循环
    for ii=1:n
        x=input('Enter value: ');
        sum1=sum1+x;
        sum2=sum2+x^2;
     end
%计算平均值和标准方差
xvar=sum1/n;
std_dev=sqrt((n*sum2-sum1^2)/(n*(n-1)));
%Print the result
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);
end

⌨️ 快捷键说明

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