本文讲解Matlab中数据分析处理相关的函数,mean平均值函数属于Matlab数据处理的基本操作之一。可以用来获取一组数据、多维数据中的平均值。文中,将给出几个实例来说明mean函数的用法。
给出mean平均值函数帮助文档如下:
>> help mean
mean Average or mean value.
S = mean(X) is the mean value of the elements in X if X is a vector.
For matrices, S is a row vector containing the mean value of each
column.
For N-D arrays, S is the mean value of the elements along the first
array dimension whose size does not equal 1.
mean(X,DIM) takes the mean along the dimension DIM of X.
S = mean(..., TYPE) specifies the type in which the mean is performed,
and the type of S. Available options are:
'double' - S has class double for any input X
'native' - S has the same class as X
'default' - If X is floating point, that is double or single,
S has the same class as X. If X is not floating point,
S has class double.
S = mean(..., MISSING) specifies how NaN (Not-A-Number) values are
treated. The default is 'includenan':
'includenan' - the mean of a vector containing NaN values is also NaN.
'omitnan' - the mean of a vector containing NaN values is the mean
of all its non-NaN elements. If all elements are NaN,
the result is NaN.
Example: If X = [1 2 3; 3 3 6; 4 6 8; 4 7 7];
then mean(X,1) is [3 4.5 6] and mean(X,2) is [2; 4; 6; 6]
Class support for input X:
float: double, single
integer: uint8, int8, uint16, int16, uint32,
int32, uint64, int64
mean函数基本用法
M = mean(A)
M = mean(A,dim)
M = mean(___,outtype)
M = mean(___,nanflag)
mean函数用法介绍
M=mean(A)返回沿第一数组维度的A元素的平均值,其大小不等于1。
- 如果A是向量,则mean(A)返回元素的平均值。
- 如果A是一个矩阵,则mean(A)返回包含每列平均值的行向量。
- 如果A是多维数组,则mean(A)沿着大小不等于1的第一个数组维度进行操作,将元素视为向量。此标注变为1,而所有其他标注的尺寸保持不变。
M=mean(A,dim)返回沿尺寸dim的平均值。例如,如果A是矩阵,则mean(A,2)是包含每行均值的列向量。
M=mean(_,outtype)使用前面语法中的任何输入参数返回具有指定数据类型的mean。outtype可以是“default”、“double”或“native”。
M=mean(_,nanflag)指定是否在前面任何语法的计算中包含或忽略NaN值。mean(A,’includenan’)包括计算中的所有NaN值,而mean(A,’omitnan’)忽略它们。
mean函数实例
矩阵列的平均值
创建一个矩阵并计算每列的平均值。
>> A = [0 1 1; 2 3 2; 1 3 2; 4 2 2]
A =
0 1 1
2 3 2
1 3 2
4 2 2
>> M = mean(A)
M =
1.7500 2.2500 1.7500
矩阵行的平均值
创建一个矩阵并计算每行的平均值。
>> A = [0 1 1; 2 3 2]
A =
0 1 1
2 3 2
>> M = mean(A,2)
M =
0.6667
2.3333
三维阵列平均值
创建1到10之间的整数的4乘2乘3数组,并沿第二维度计算平均值。
>> A = gallery('integerdata',10,[4,2,3],1)
A(:,:,1) =
10 9
8 5
10 9
6 6
A(:,:,2) =
1 2
4 4
9 6
5 10
A(:,:,3) =
6 8
4 1
1 7
8 3
>> M = mean(A,2)
M(:,:,1) =
9.5000
6.5000
9.5000
6.0000
M(:,:,2) =
1.5000
4.0000
7.5000
7.5000
M(:,:,3) =
7.0000
2.5000
4.0000
5.5000
单精度阵列的平均值
创建一个单精度向量,并计算其单精度平均值。
>> A = single(ones(10,1));
>> M = mean(A,'native')
M =
1
结果也是单精度的。
>> class(M)
ans =
single
不含NaN的平均值
创建一个向量并计算其平均值,不包括NaN值。
>> A = [1 0 0 1 NaN 1 NaN 0];
>> M = mean(A,'omitnan')
M =
0.5000
如果未指定“omitnan”,则mean(A)返回NaN。
原创文章,作者:古哥,转载需经过作者授权同意,并附上原文链接:https://iymark.com/articles/3769.html