[转载]matlab求矩阵某一列的最大值及所在位置
2018-07-24 20:32阅读:
第一种:
clear;clc;
A=[0 17 50;-12 40 3;5 -10 2;30 4 3]
[C,I]=max(A(:))
[m,n]=ind2sub(size(A),I)
运行结果:
A =
0
17
50
-12
40
3
5
-10
2
30
4
3
C =
50
I =
9
m =
1
n =
3
第二种:
clear;clc;
A=[0 17 50;-12 40 3;5 -10 2;30 4 3]
[M,I]=max(A)
[N,J]=max(M)
[I(J),J]
运行结果:
A =
0
17
50
-12
40
3
5
-10
2
30
4
3
M =
30
40
50
I =
4
2
1
N =
50
J =
3
ans =
1
3
第三种:
clear;clc;
A=[0 17 50;-12 40 3;5 -10 2;30 4 3]
N=max(max(A))
%或者N=max(A(:))
[r,c]=
find(N==A)
运行结果:
A =
0
17
50
-12
40
3
5
-10
2
30
4
3
N =
50
r =
1
c =
3
MATLAB的矩阵元素顺序是按列排列的,也就是说index=9的一个元素在5*6的矩阵中处于(4,2)的位置,而在3*10的矩阵中处于(3,3)的位置。
ind2sub函数就是在指定矩阵尺寸(size)前提下将给定的index转化成行列形式
[I,J] = ind2sub(siz,IND) returns
the matrices I and J containing the equivalent row and column
subscripts corresponding to each linear index in the matrix IND for
a matrix of size siz. siz is a 2-element vector, where siz(1) is
the number of rows and siz(2) is the number of
columns.(也是反映元素位置的)
For matrices, [I,J] =
ind2sub(size(A),find(A>5)) returns the same values as [I,J] =
find(A>5)
A=magic(4)
[Y_col,Ind_row]=max(A)%每列的最大值及行号
[Y_row,Ind_col]=max(A')%每行的最大值及列号
>> a=[1,2,3;4,5,6;7,4,2]
a =
1
2
3
4
5
6
7
4
2
>> a(:)
ans =
1
4
7
2
5
4
3
6
2
>> find(a>4)
ans =
3
5
8
下标指数
>> [i,j]=find(a>4)
[m,n]=find(A)或n=find(A)——返回矩阵A中非0项的坐标
通常与逻辑运算符一起使用,
如[m,n]=find(B>2)
返回的是矩阵B中大于2的坐标。
i =
3
2
2
j =
1
2
3
(1)
a=find(A)返回数组A中非零元素的单下标索引
(2)[a,b]=find(A)返回数组A中非零元素的双下标索引放方式。