细说MATLAB中的MAX函数 (翻译Help文档而来)

细说MATLAB中的MAX函数

前言:上午在使用OMP算法的时候,碰到[max,pos]=max(abs(a))这么个表达,一时并不知道前面那两个代表什么玩意,于是就翻阅了下help文档,稍微研究了下,就权当做个笔记,废话不说了,开始正题:

一、MAX函数的几种形式
(1)C = max(A)
(2)C = max(A,B)
(3)C = max(A,[],dim)
(4)[C,I]=max(A)

二、详解各个函数意思
(1)C=max(A)

C = max(A) returns the largest elements along different dimensions of an array.
If A is a vector, max(A) returns the largest element in A.
If A is a matrix, max(A) treats the columns of A as vectors, returning a row vector containing the maximum element from each column.
If A is a multidimensional array, max(A) treats the values along the first non-singleton dimension as vectors, returning the maximum value of each vector. 

Help文档上将A分为矩阵、数组等来分别阐述,其实意思很明显,比如:如果A是一个矩阵,比如A=[1,2,3;4,5,6],max(A)的意思就是找出矩阵每列的最大值, 本例中:max(A)=[4,5,6]

(2)C=max(A,B)

C = max(A,B) returns an array the same size as A and B with the largest elements taken from A or B. The dimensions of A and B must match, or they may be scalar.

解释:如果a和b都是大于1维的矩阵,那么要求a和b的行列的维数都要相等,函数的结果是比较a和b中每个元素的大小,比如:
A=[1,2,3;4,5,6] B=[4,5,6;7,8,3] max(A,B) =[4,5,6;7,8,6] 另外,如果A和B中至少有一个是常数,也是可以的。比如: A=[1,2,3;4,5,6] B=3 max(A,B) =[3,3,3;4,5,6]!

(3)C=max(A,[],dim)

C = max(A,[],dim) returns the largest elements along the dimension of A specified by scalar dim. For example, max(A,[],1) produces the maximum values along the first dimension of A.

解释:这个函数的意思是针对于2维矩阵的,dim意思是维数。 当dim=1时,比较的a矩阵的行,也就是和max(A)的效果是一样的;当dim=2时,比较A矩阵的列。下面举个Eg:
a=[1,2,3;4,5,6] max(a)=max(a,[],1)=[4,5,6] 比较的第一行和第二行的值 max(a,[],2)=[3,6]

(4)[C,I]=max(A)

[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I. If there are several identical maximum values, the index of the first one found is returned.

解释:C表示的是矩阵A每列的最大值,I表示的是每个最大值对应的下标: 下面举例说明: 还是刚才那个例子:A=[1,2,3;4,5,6] [C,I]=max(A) 结果显示的是C=[4,5,6] I=[2,2,2] 返回的是最大值对应的行号。

到此,解释完毕,不说什么精辟的言论,但学习MATLAB一定得借助自带的help文档一定不会错!!!

你可能感兴趣的:(matlab,max函数)