find the location of the n largest (non-unique) values in a matrix
7 views (last 30 days)
Show older comments
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 Comment
Cedric
on 20 Sep 2017
Edited: Cedric
on 20 Sep 2017
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.
Accepted Answer
the cyclist
on 20 Sep 2017
Edited: the cyclist
on 20 Sep 2017
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))
More Answers (1)
Steven Lord
on 21 Sep 2017
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!