【发布时间】:2013-04-16 15:47:36
【问题描述】:
我有一个与这里解决的问题非常相似的问题:
Get the indices of the n largest elements in a matrix
但是,此解决方案将矩阵转换为数组,然后以新数组的形式给出索引。
我想要原始矩阵的行和列索引的最大(和最小)n 值。
【问题讨论】:
标签: matlab
我有一个与这里解决的问题非常相似的问题:
Get the indices of the n largest elements in a matrix
但是,此解决方案将矩阵转换为数组,然后以新数组的形式给出索引。
我想要原始矩阵的行和列索引的最大(和最小)n 值。
【问题讨论】:
标签: matlab
如果您采用该问题中的解决方案来找到 5 个最大的唯一值
sortedValues = unique(A(:)); %# Unique sorted values
maxValues = sortedValues(end-4:end); %# Get the 5 largest values
maxIndex = ismember(A,maxValues); %# Get a logical index of all values
%# equal to the 5 largest values
为您提供匹配的值的逻辑矩阵。您可以使用find 获取它们的索引,然后使用ind2sub 将它们转换回坐标。
idx = find(maxIndex);
[x y] = ind2sub(size(A), idx);
根据 cmets 的替代方案:
[foo idx] = sort(A(:), 'descend'); %convert the matrix to a vector and sort it
[x y] = ind2sub(size(A), idx(1:5)); %take the top five values and find the coords
注意:上述方法不会消除任何重复值,因此例如,如果您有两个具有相同值的元素,它可能会返回两个元素,或者如果它们在边界上,则只返回两者之一。
【讨论】:
ind2sub。