主要内容

最大限度

数组的最大元素

描述

example

M= max(A)返回数组的最大元素。

  • 如果A是一个向量,然后最大(A)returns the maximum ofA

  • 如果A是矩阵,然后最大(A)is a row vector containing the maximum value of each column.

  • 如果A是一个多维数组,然后最大(A)operates along the first array dimension whose size does not equal1,将元素视为向量。这个维度的大小变成1尽管所有其他维度的尺寸保持不变。如果A是一个empty array whose first dimension has zero length, then最大(A)返回一个与相同大小的空数组A

example

M= max(A,[],dim)返回沿维度的最大元素dim。例如,如果A是矩阵,然后最大限度(A,[],2)是包含每一行的最大值的列向量。

example

M= max(A,[],Nanflag)指定包括还是省略NaN值the calculation. For example,max(a,[],'includeenan')includes allNaNA尽管最大限度(A,[],'omitnan')ignores them.

M= max(A,[],dim,Nanflag)还指定使用时要运行的维度Nanflag选项。

example

[M,I] = max(___)also returns the index into the operating dimension that corresponds to the maximum value ofAfor any of the previous syntaxes.

example

M= max(A,[],'all')在所有元素上找到最大A。This syntax is valid for MATLAB®versions R2018b and later.

example

M= max(A,[],Vecdim)计算在向量中指定的尺寸上的最大值Vecdim。例如,如果A是矩阵,然后最大限度(A,[],[1 2])computes the maximum over all elements inA,由于矩阵的每个元素都包含在尺寸1和2定义的阵列切片中。

M= max(A,[],'all',Nanflag)计算最大A什么时候using theNanflag选项。

M= max(A,[],Vecdim,Nanflag)指定使用时要运行的多个维度Nanflag选项。

[M,I] = max(A,[],'all',___)将线性索引返回到A对应于最大值A指定时'all'

example

[M,I] = max(A,[],___,'linear')将线性索引返回到A对应于最大值A

example

C= max(A,B)返回一个阵列,其中最大的元素取自A或者B

C= max(A,B,Nanflag)也指定如何治疗NaN值。

___= max(___,“比较method”,方法)可选地指定如何比较任何先前语法的元素。例如,对于向量A = [-1 2 -9], the syntaxmax(a,[],'comparionmethod','abs')比较Aaccording to their absolute values and returns-9

例子

collapse all

创建向量并计算其最大元素。

a = [23 42 37 18 52];m = max(a)
M = 52

创建一个复杂的向量并计算其最大元素,即具有最大幅度的元素。

A = [-2+2i 4+i -1-3i]; max(A)
ans = 4.0000 + 1.0000i

创建一个矩阵并计算每列中最大的元素。

a = [2 8 4;7 3 9]
A =2×32 8 4 7 3 9
m = max(a)
M =1×37 8 9

创建一个矩阵并计算每一行中最大的元素。

a = [1.7 1.2 1.5;1.3 1.6 1.99]
A =2×31.7000 1.2000 1.5000 1.3000 1.6000 1.9900
m = max(a,[],,2)
M =2×11.7000 1.9900

创建向量并计算其最大值,不包括NaN值。

a = [1.77 -0.005 3.98 -2.95 Nan 0.34 Nan 0.19];m = max(a,[],,'omitnan')
M = 3.9800

最大(A)也会产生这个结果如果nce'omitnan'是默认选项。

使用'includenan'flag to returnNaN

m = max(a,[],,'includenan')
m = nan

创建一个矩阵A并计算每一列中最大的元素,以及Ain which they appear.

A = [1 9 -2; 8 4 -5]
A =2×31 9 -2 8 4 -5
[m,i] = max(a)
M =1×38 9 -2
i =1×32 1 1

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

a(::,:,1)= [2 4;-2 1];a(::,:,2)= [9 13;-5 7];a(::,:,3)= [4 4;8 -3];m1 = max(a,[],[1 2])
m1 = m1(:,:,1)= 4 m1(:,:,:,2)= 13 m1(:,,:,:,3)= 8

Starting in R2018b, to compute the maximum over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the'all'选项。

m2 = max(a,[],[1 2 3])
M2 = 13
Mall = max(A,[],'all')
购物中心= 13

创建一个矩阵A并返回矩阵中每一行的最大值M。使用“线性”option to also return the linear indicesI这样M = A(I)

a = [1 2 3;4 5 6]
A =2×31 2 3 4 5 6
[m,i] = max(a,[],2,“线性”)
M =2×13 6
i =2×15 6
maxVals = a(i)
maxvals =2×13 6

与标量相比,创建一个矩阵并返回其每个元素之间的最大值。

a = [1 7 3;6 2 9]
A =2×31 7 3 6 2 9
b = 5;c = max(a,b)
C =2×35 7 5 6 5 9

Input Arguments

collapse all

输入数组, specified as a scalar, vector, matrix, or multidimensional array.

  • 如果A很复杂,然后最大(A)返回最大的复数数。如果幅度相等,则最大(A)returns the value with the largest magnitude and the largest phase angle.

  • 如果A是标量最大(A)returnsA

  • 如果A是一个0乘0的空数组,然后最大(A)也是。

如果A有类型分类,那么它必须是序数。

数据类型:单身的|双倍的|int8|INT16|INT32|INT64|UINT8|UINT16|UINT32|Uint64|logical|分类|约会时间|duration
Complex Number Support:是的

沿着操作的维度,指定为正整数标量。如果未指定值,则默认值是第一个数组维度,其大小不等于1。

方面dimindicates the dimension whose length reduces to1。这尺寸(M,昏暗)is1, while the sizes of all other dimensions remain the same, unlesssize(A,dim)is0。如果size(A,dim)is0, thenmax(a,昏暗)返回一个与相同大小的空数组A

考虑二维输入阵列,A:

  • 如果DIM = 1, then最大限度(A,[],1)returns a row vector containing the largest element in each column.

  • 如果DIM = 2, then最大限度(A,[],2)returns a column vector containing the largest element in each row.

最大限度returnsAifdim大于ndims(a)

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。然后最大限度(A,[],[1 2])returns a 1-by-1-by-3 array whose elements are the maximums computed over each page ofA

数据类型:双倍的|单身的|int8|INT16|INT32|INT64|UINT8|UINT16|UINT32|Uint64

附加输入阵列,指定为标量,向量,矩阵或多维数组。输入ABmust either be the same size or have sizes that are compatible (for example,A是一个M-经过-N矩阵和B是标量或1-经过-Nrow vector). For more information, seeCompatible Array Sizes for Basic Operations

  • AB除非一个是双倍的。In that case, the data type of the other array can be单身的,duration,或任何整数类型。

  • 如果AB是有序的分类数组,它们必须具有相同的类别集,并具有相同的顺序。

数据类型:单身的|双倍的|int8|INT16|INT32|INT64|UINT8|UINT16|UINT32|Uint64|logical|分类|约会时间|duration
Complex Number Support:是的

NaN条件,指定为以下值之一:

  • 'omitnan'- 忽略所有NaN输入中的值。如果所有元素是NaN, then最大限度返回第一个。

  • 'includenan'- 包括NaN计算输入中的值。

For约会时间数组,您也可以使用'omitnat'或者'includeNat'to omit and includeNaT值分别。

For分类数组,您也可以使用``省略定义''或者“包含定义”to omit and include undefined values, respectively.

数据类型:char

数字输入的比较方法,指定为以下值之一:

  • 'auto'— For a numeric input arrayA, compare elements by真实(A)什么时候A是真实的,ABS(A)什么时候Ais complex.

  • '真实的'— For a numeric input arrayA, compare elements by真实(A)什么时候A是真实的或复杂的。如果A具有相等的真实零件的元素,然后使用imag(A)to break ties.

  • 'abs'— For a numeric input arrayA, compare elements byABS(A)什么时候A是真实的或复杂的。如果A具有相等大小的元素,然后使用angle(A)在间隔(-π,π)中打破领带。

输出参数

collapse all

最大值,作为标量,向量,矩阵或多维数组返回。尺寸(M,昏暗)is1,而所有其他维度的大小与相应维度的大小相匹配A, 除非size(A,dim)is0。如果size(A,dim)is0, thenM是一个空数阵列,大小与A

Index, returned as a scalar, vector, matrix, or multidimensional array.I与第一个输出相同。

什么时候“线性”未指定,I是操作维度的索引。什么时候“线性”is specified,I包含线性索引A对应于最大值。

如果the largest element occurs more than once, thenIcontains the index to the first occurrence of the value.

最大元素来自A或者B,作为标量,向量,矩阵或多维数组返回。的大小Cis determined by implicit expansion of the dimensions ofAB。有关更多信息,请参阅Compatible Array Sizes for Basic Operations

数据类型C取决于数据类型AB:

  • 如果ABare the same data type, thenC匹配数据类型AB

  • 如果是A或者Bis单身的, thenCis单身的

  • 如果是A或者B是一个integer data type with the other a scalar双倍的, thenCassumes the integer data type.

扩展功能

在R2006a之前引入