フィルターのクリア

Finding the position of the 1st,2nd and 3rd max value in a matrix

56 ビュー (過去 30 日間)
Amin
Amin 2011 年 11 月 29 日
回答済み: Ishtiaq Khan 2021 年 11 月 10 日
Hi, I want to find the position of the 1st,2nd and 3rd maximum value of a matrix.I know that I can find the position of the max value using find() function like:(e.g. X is a matrix)
[i j]=find(X==max(X))
but it gives just the position of max value.
Thanks,
Amin.
  2 件のコメント
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
What do you want to have happen if there are duplicate copies of the maximum?
Amin
Amin 2011 年 11 月 29 日
Walter,
This is good question...I have no idea!what do you think?

サインインしてコメントする。

採用された回答

Jan
Jan 2011 年 11 月 29 日
If X is not unique, find(X==max(X)) can find more than one element. Then Sven's sort method yields to another reply.
For large arrays sorting is expensive. You can try this:
[max1, ind1] = max(X);
X(ind1) = -Inf;
[max2, ind2] = max(X);
X(ind2) = -Inf;
[max3, ind3] = max(X);
X(ind3) = -Inf;
For X = rand(1, 1e6) this is 4.7 times faster than the SORT-method under Matlab 2009a, Win7/64.
  3 件のコメント
Sven
Sven 2011 年 11 月 30 日
Amin, note the "X(:)" part of my answer. Using "(:)" will ensure that max() flattens an n-by-m matrix into a p-by-1 matrix for the purposes of finding its maximum. My ind2sub() command returns the row/column index from the linear index returned by find().
surendra bala
surendra bala 2018 年 1 月 30 日
Thanks. That's a good idea

サインインしてコメントする。

その他の回答 (5 件)

Sven
Sven 2011 年 11 月 29 日
Hi Amin, try this:
[sortedX, sortedInds] = sort(X(:),'descend');
top3 = sortedInds(1:3)
And if you want to get the (i,j) reference into X, just follow with:
[i, j] = ind2sub(size(X), top3);
Here's a general solution (ala Jan) for the N maximum numbers that will be faster than sort() if you have a (very) large matrix X:
N = 10;
inds = zeros(N,1);
tmpX = X(:);
for i=1:N
[~, inds(i)] = max(tmpX);
tmpX(inds(i)) = -inf;
end
[rows, cols] = ind2sub(size(X), inds);
Note that in my opinion, I'd need X be very large or my calculation to be performed many times in a loop before I'd consider the (simpler) sort() method to be too inefficient.

Edwin Fonkwe
Edwin Fonkwe 2011 年 11 月 29 日
You could run the "find()" function three times. After each time, replace the previously found max in the matrix by a very small number (probably less than the minimum). Hope this helps
  2 件のコメント
Walter Roberson
Walter Roberson 2011 年 11 月 29 日
if it was a floating point array, you could replace it with NaN instead of a small number.
Amin
Amin 2011 年 11 月 29 日
FONKWE and Walter,
Thank you for your responses.
Amin.

サインインしてコメントする。


Thang Vu
Thang Vu 2019 年 1 月 22 日
編集済み: Thang Vu 2019 年 1 月 22 日
function [U, I] = Xmax(X, i) % i is the x-largest value
for j = 1: i-1
[U, I] = max(X);
X(I) = -Inf;
[U, I] = max(X);
end
after each round you find and change the maximum number to -Inf

Niño Dong Won Shin
Niño Dong Won Shin 2020 年 10 月 6 日
sampleData = ["Samsung Note 9","59","53900";
"Samsung S20 Ultra","150","69900";
"Samsung S10","200","55900";
"Samsung Note 10","46","54900";
"IPhone 11","45","40990"];
%Extract 1st Column
Item =
%Extract 2nd Column and make it as a vector array
Unit =
%Extract 3rd Column and make it as a vector array
UnitCost =
TotalCost =
%Search the most high priced item in the inventory and its current index
[max,index] =
searchMax =
% Tell user

Ishtiaq Khan
Ishtiaq Khan 2021 年 11 月 10 日
The following would given you positions of all elements in one-dimentional array X. For a matrix, you can do a little bit tweaking.
[~,idx] = sort(X);
[~,idx]=sort(idx);
idx=numel(X)+1-idx;

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by