フィルターのクリア

How to find the 5th highest value in a matrix

2 ビュー (過去 30 日間)
Smit Patel
Smit Patel 2020 年 1 月 29 日
コメント済み: Smit Patel 2020 年 1 月 29 日
I am trying to find the 5th highest value of a two-dimensional matrix.
For ex)
t_1=rand(3,3)
t_2=unique(t_1)
t_3=maxk(t_2,5)
So I tried that but it gives me all the 5 values. I don't want all 5 in decreasing order till i get 5th highest. I just want 5th highest value. I thought of using this:
t_1=rand(3,3)
t2=sort(t_1,'descend')
t3=t2(5)
But this is only gonig to work if its 3by3. what if it was some big 5by5 or 7by7. I can't use that. It wont give me the fifth highest. or will it?

採用された回答

John D'Errico
John D'Errico 2020 年 1 月 29 日
編集済み: John D'Errico 2020 年 1 月 29 日
Can you convert a fully generally shaped array into a vector? Hint: What does t(:) do?
That works on an array of any shape or size. Since it does convert an array into a vector, then does your sort trick now work, ALWAYS? (Yes.)
You could also have used the reshape function, but that takes slightly more typing, so why bother? Still only one line to do so, but we need to conserve, even if it is only a few extra bits of text. ;-)
  1 件のコメント
Smit Patel
Smit Patel 2020 年 1 月 29 日
Thank you so much, @John D'Errico! I really appreciate it!

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

その他の回答 (0 件)

カテゴリ

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