find the location of the n largest (non-unique) values in a matrix
7 次查看(过去 30 天)
显示 更早的评论
I'm trying to write some code that generates a n x 2 matrix that describes the location of the n largest values in a large matrix called "BinConcentrationCopy" (which is a 12 x 24 matrix). Here's the code I've been working with so far:
for n = 1:5
[row(n), column(n)] = find(BinConcentrationCopy==max(max(BinConcentrationCopy)));
BinConcentrationCopy(row(n), column(n)) = 0;
end
location = [row; column];
This code works perfectly and generates the correct 5x2 "location" matrix if the 5 largest values in my BinConcentrationCopy are all unique numbers; however, this isn't always the case. If, for example, the 3rd largest value in BinConcentrationCopy shows up twice (aka it's the 3rd and the 4th largest value for that matrix) then an error occurs and the for loop quits before iterating all the way through. Any ideas on how to debug this? Thanks in advance.
1 个评论
Cedric
2017-9-20
编辑:Cedric
2017-9-20
When FIND finds multiple occurrences of non-false/zero elements in the array passed as first argument, it outputs vectors of corresponding rows and columns. These vectors cannot be stored in row(n) and column(n) because they are numeric arrays: n indexes a single element.
You can define row and col as cell arrays:
row = cell( 5, 1 ) ;
col = cell( 5, 1 ) ;
and store the output of FIND in cells:
[row{n},col{n}] = ...
Note the curly-bracket indexing here, that means "content of cell", i.e. row{n} accesses the "content of cell n", when row(n) would access "cell n".
In the end, you may have to concatenate their content.
采纳的回答
the cyclist
2017-9-20
编辑:the cyclist
2017-9-20
Here is a quite different approach:
% Some pretend data with a duplicated max value
BinConcentrationCopy = magic(7);
BinConcentrationCopy(1,1) = 49;
% The algorithm
n = 5;
[~,sortingIndex] = sort(BinConcentrationCopy(:),'desc')
[row, col] = ind2sub(size(BinConcentrationCopy),sortingIndex(1:n))
更多回答(1 个)
Steven Lord
2017-9-21
Release R2017b introduced a new function named maxk that you can use for this purpose. Let me generate some sample data.
rng default
x = randi(10, 1, 100);
Check how many elements of x are equal to the maximum value (which is 10, by the way I constructed x.)
nMax = nnz(x == max(x));
Let's retrieve those fifteen 10's (nMax is 15) plus the next 5 largest values.
[B, I] = maxk(x, nMax+5);
If you look at B, you will see that it contains fifteen 10's as well as five 9's.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!