Main Content

mean

数组的平均值或平均值

Description

例子

m =平均值(一种返回mean元素一种沿着大小不等于1的第一个数组尺寸。

  • If一种一世s a vector, thenmean(A)返回元素的平均值。

  • If一种一世s a matrix, thenmean(A)返回a row vector containing the mean of each column.

  • If一种一世s a multidimensional array, thenmean(A)沿着大小不等于1的第一个数组维度操作,将元素视为向量。这个维度变成了1while the sizes of all other dimensions remain the same.

例子

m =平均值(一种,,,,'all'computes the mean over all elements of一种。此语法对MATLAB有效®版本R2018B及以后。

例子

m =平均值(一种,,,,暗淡返回沿尺寸的平均值暗淡。为了例子,,,,一世f一种一世s a matrix, thenmean(A,2)一世s a column vector containing the mean of each row.

例子

m =平均值(一种,,,,vecdimcomputes the mean based on the dimensions specified in the vectorvecdim。为了例子,,,,一世f一种一世s a matrix, then平均(a,[1 2])一世s the mean of all elements in一种,由于矩阵的每个元素都包含在尺寸1和2定义的阵列切片中。

例子

m =平均值(___,,,,outtype返回mean with a specified data type, using any of the input arguments in the previous syntaxes.outtype'default',,,,'double', 或者'本国的'

例子

m =平均值(___,,,,nanflagspecifies whether to include or omit来自任何先前语法的计算值。平均(a,'includeenan')一世ncludes allvalues in the calculation whilemean(A,'omitnan')忽略它们。

Examples

collapse all

创建一个矩阵并计算每列的平均值。

a = [0 1 1;2 3 2;1 3 2;4 2 2]
a =4×30 1 1 2 3 2 1 3 2 4 2 2
M =平均(A)
M =1×31.7500 2.2500 1.7500

创建一个矩阵并计算每一行的均值。

a = [0 1 1;2 3 2;3 0 1; 1 2 3]
a =4×30 1 1 2 3 2 3 0 1 1 2 3
m =平均值(a,2)
M =4×10.6667 2.3333 1.3333 2.0000

Create a 4-by-2-by-3 array of integers between 1 and 10 and compute the mean values along the second dimension.

rng('default')a = randi(10,[4,2,3]);m =平均值(a,2)
m = m(:,:,1)= 8.0000 5.5000 2.5000 8.0000 m(:,::,:,2)= 10.0000 7.5000 5.5000 6.0000 m(::,:,:,3)= 6.0000 5.5000 8.5000 10.5000 10.0000

创建一个3-D数组,并在数据的每个页面上计算平均值(行和列)。

a(::,:,1)= [2 4;-2 1];a(::,:,2)= [9 13;-5 7];a(::,:,3)= [4 4;8 -3];m1 =平均值(a,[1 2])
M1 = M1 (:: 1) = 1.2500 M1 (:,: 2) = 6 M1 (:,:, 3) =3.2500

从R2018B开始,为了计算数组的所有维度的平均值,您可以指定向量维度参数中的每个维度,也可以使用'all'option.

m2 =平均值(a,[1 2 3])
M2 = 3.5000
购物中心=平均值(a,'all'
购物中心= 3.5000

Create a single-precision vector of ones and compute its single-precision mean.

a =单身的(ones(10,1)); M = mean(A,'本国的'
M =单身的1

结果也单一精确。

班级(M)
ans = 'single'

创建向量并计算其平均值,不包括值。

a = [1 0 0 1 nan 1 nan 0];m =平均值(a,'omitnan'
M = 0.5000

如果您不指定'omitnan', 然后mean(A)返回

输入参数

collapse all

输入阵列,指定为向量,矩阵或多维数组。

  • If一种一世s a scalar, thenmean(A)返回一种

  • If一种是一个空的0 x-0矩阵,然后mean(A)返回

数据类型:单身的|double|int8|一世nt16|一世nt32|INT64|UINT8|uint16|UINT32|Uint64|逻辑|char|datetime|期间

沿着操作的维度,,,,specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Dimension暗淡指示长度降低到的维度1。这尺寸(M,昏暗)一世s1,,,,while the sizes of all other dimensions remain the same.

Consider a two-dimensional input array,一种

  • IfDIM = 1, 然后平均(A,1)返回包含每列元素的平均值的行矢量。

  • IfDIM = 2, 然后mean(A,2)返回包含每行元素的平均值的列向量。

mean返回一种什么时候暗淡一世s greater thanndims(A)或何时大小(a,昏暗)一世s1

数据类型:double|单身的|int8|一世nt16|一世nt32|INT64|UINT8|uint16|UINT32|Uint64

Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.

考虑一个2 x-3 x-3输入阵列,一种。然后平均(a,[1 2])返回一个1 x-1 x-3数组,其元素是每页上的均值一种

数据类型:double|单身的|int8|一世nt16|一世nt32|INT64|UINT8|uint16|UINT32|Uint64

输出数据类型,指定为'default',,,,'double', 或者'本国的'。这se options also specify the data type in which the operation is performed.

outtype Output data type
'default' double,,,,unless the input data type is单身的,,,,期间, 或者datetime,在这种情况下,输出为'本国的'
'double' double,,,,unless the data type is期间ordatetime, 在这种情况下,'double'一世s not supported
'本国的' 与输入相同的数据类型,除非
  • 输入数据类型是逻辑,在这种情况下,输出为double

  • 输入数据类型是char, 在这种情况下,'本国的'一世s not supported

数据类型:char

condition, specified as one of these values:

  • 'includeenan'- 包括计算平均值时的值

  • 'omitnan'-Ignore allvalues in the input.

为了datetime数组,您也可以使用'omitnat'or'includenat'省略并包括纳特值分别。

数据类型:char

更多关于

collapse all

Mean

对于随机变量向量一种made up ofn标量观察,平均值定义为

μ = 1 n 一世 = 1 n 一种 一世

扩展功能

也可以看看

||||

Introduced before R2006a