如何在MATLAB中获得1D数组中"n个最小元素"的索引?
该数组是行向量.
我可以找到最小元素及其索引;
[C, ind] = min(featureDist);
Run Code Online (Sandbox Code Playgroud)
矢量如下:
featureDist =
Columns 1 through 8
48.4766 47.3743 59.5736 59.7450 55.0489 58.2620 63.3865 50.1101
Run Code Online (Sandbox Code Playgroud)
等等...
Heb*_*odo 22
你可以使用这个sort
功能.要获得最小的n个元素,可以编写如下函数:
function [smallestNElements smallestNIdx] = getNElements(A, n)
[ASorted AIdx] = sort(A);
smallestNElements = ASorted(1:n);
smallestNIdx = AIdx(1:n);
end
Run Code Online (Sandbox Code Playgroud)
让我们试试你的阵列:
B = [48.4766 47.3743 59.5736 59.7450 55.0489 58.2620 63.3865 50.1101];
[Bsort Bidx] = getNElements(B, 4);
Run Code Online (Sandbox Code Playgroud)
回报
BSort =
47.3743 48.4766 50.1101 55.0489
Bidx =
2 1 8 5
Run Code Online (Sandbox Code Playgroud)