找到矩阵中的最高/最低值

koj*_*rac 4 matlab

非常基本的问题:如何在随机矩阵中找到最高或最低值.我知道有可能说:

a = find(A>0.5)
Run Code Online (Sandbox Code Playgroud)

但我正在寻找的更像是这样的:

A = rand(5,5)
A = 
0.9388    0.9498    0.6059    0.7447    0.2835
0.6338    0.0104    0.5179    0.8738    0.0586
0.9297    0.1678    0.9429    0.9641    0.8210
0.0629    0.7553    0.7412    0.9819    0.1795
0.3069    0.8338    0.7011    0.9186    0.0349

% find highest (or lowest) value

ans = A(19)%for the highest or A(7) %for the lowest value in this case
Run Code Online (Sandbox Code Playgroud)

Jon*_*erg 16

看看min()max()功能.它们可以返回最高/最低值及其索引:

[B,I]=min(A(:)); %# note I fixed a bug on this line!
Run Code Online (Sandbox Code Playgroud)

回归I=7B=A(7)=A(2,2).该表达式A(:)告诉MATLAB暂时将A视为一维数组,因此即使A为5x5,它也会返回线性索引7.

如果您需要2D坐标,即"2,2" B=A(7)=A(2,2),您可以使用[I,J] = ind2sub(size(A),I)哪个返回I=2,J=2,请参见此处.

更新
如果您需要所有条目的索引达到最小值,您可以使用find:

I = find(A==min(A(:));
Run Code Online (Sandbox Code Playgroud)

I 现在是所有这些的矢量.