How can I find the max value and location within a matrix
2 ビュー (過去 30 日間)
古いコメントを表示
I am working with a 7783x1 matrix, and I am trying to find the max value & its location. But I need to do it in the script and not in the command window. I have the matrix data saved as a FILE.mat in the same folder.
0 件のコメント
採用された回答
Image Analyst
2021 年 1 月 23 日
Try this, where m is your matrix
maxValue = max(m(:));
rows = find(m == maxValue); % Find all locations where m equals the max value of m
5 件のコメント
Image Analyst
2021 年 1 月 23 日
You did not change the names like I said. Leave the semicolon off line 17 and see what fields it shows in the command window and use that instead of m.
その他の回答 (1 件)
Adam Danz
2021 年 1 月 23 日
[maxval, idx] = max(M);
2 件のコメント
Image Analyst
2021 年 1 月 23 日
Only finds the FIRST occurrence, not all of them. See how I did it if you want to make sure you find all of them.
Adam Danz
2021 年 1 月 23 日
That's true, but I'd recommend using logical indexing rather than slowing it down with find()
maxval = max(M);
idx = M==maxval;
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!