How can I find the index of the maximum value in a matrix column in MATLAB? [duplicate]

和自甴很熟 提交于 2019-12-19 05:18:30

问题


I'm trying to find the maximum value of a certain column in a matrix. I want to find both the maximum value and the index of the row in which it is. How can I do this?


回答1:


max command can find both the maximal value and its index.
Here's an example:

>> A = randn(10,3)
A = 
       0.8884     -0.10224     -0.86365
      -1.1471     -0.24145     0.077359
      -1.0689      0.31921      -1.2141
      -0.8095      0.31286      -1.1135
      -2.9443     -0.86488   -0.0068493
       1.4384    -0.030051       1.5326
      0.32519     -0.16488     -0.76967
     -0.75493      0.62771      0.37138
       1.3703       1.0933     -0.22558
      -1.7115       1.1093       1.1174

>> [maxVal maxInd] = max(A)
maxVal =
       1.4384       1.1093       1.5326
maxInd =
     6    10     6



回答2:


If you want the maximum of a specific column, you only pass that column to max, or you select the column from the resulting list of indices.

%# create an array
A = magic(4)

A =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

%# select the maximum of column 3
[maxValue, rowIdx] = max(A(:,3),[],1)

maxValue =
    15
rowIdx =
     4

If you need to look up a corresponding value in another array, you use otherArray(rowIdx,3)



来源:https://stackoverflow.com/questions/3673804/how-can-i-find-the-index-of-the-maximum-value-in-a-matrix-column-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!