I want to find second and third maxima in a row of a matrix of size 1000*8

1 回表示 (過去 30 日間)
RAJAT
RAJAT 2023 年 6 月 4 日
編集済み: Image Analyst 2023 年 6 月 4 日
Can someone please send me the code to find second and third maximum no. in a particular row of a matrix size 1000*8?? Thanks

採用された回答

Image Analyst
Image Analyst 2023 年 6 月 4 日
編集済み: Image Analyst 2023 年 6 月 4 日
Use maxk:
m = randi(99, 4, 10) % Sample small matrix. Replace with yours.
m = 4×10
21 48 46 51 24 54 28 96 36 64 66 3 36 18 43 35 68 80 63 57 9 63 9 28 12 59 36 71 14 33 82 24 14 35 50 91 40 62 96 90
row = 2; % Whatever row you want.
v2 = maxk(m(row, :), 3)
v2 = 1×3
80 68 66
v2 = v2(2:3) % Take second and third biggest only. Ignore the largest.
v2 = 1×2
68 66

その他の回答 (1 件)

John D'Errico
John D'Errico 2023 年 6 月 4 日
編集済み: John D'Errico 2023 年 6 月 4 日
BREAK DOWN PROBLEMS THAT ARE TOO LARGE FOR YOU TO HANDLE INTO SMALLER, BITE SIZED CHUNKS.
First, if it is a particular row, then trivially, all you care about is a VECTOR, since you can extract that row.
How can you find the second and third largest elements in a VECTOR? What would happen if you sort the vector? Where would the second and third largest elements fall? Sort tells you that information. It even tells you where the elements originally fell in the vector. And you can tell sort to return a sorted order in increasing or decreasing order. That is an option for sort.
So you can solve everything you need, by making a large problem into smaller bite sized problems. If each of them is too large, then break them down in turn, until the large problem is no longer large. Learn to use the tools in MATLAB.
Eat a programming elephant one byte at a time.

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by