Finding the indices of a max value of a matrix

148 ビュー (過去 30 日間)
James Robert Fletcher
James Robert Fletcher 2020 年 6 月 23 日
コメント済み: Walter Roberson 2020 年 6 月 24 日
I have a matrix(1001x251) and i need to find where the maximum value occurs in each row using a for loop and the find function. please help!!
  3 件のコメント
vikal kumar
vikal kumar 2020 年 6 月 24 日
use this command
[max,idx]=max(m)
Walter Roberson
Walter Roberson 2020 年 6 月 24 日
[max,idx]=max(m, [], 2);

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

回答 (2 件)

Chaitanya Bidnur
Chaitanya Bidnur 2020 年 6 月 24 日
The function is max. To obtain the first maximum value you should do
[val, idx] = max(a);
val is the maximum value and idx is its index.
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 6 月 24 日
max() in that form finds the maximum value per column, not per row.

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


Walter Roberson
Walter Roberson 2020 年 6 月 24 日
When you are at the beginning of any row:
  • if the row is empty, then the max for the row is empty
  • otherwise, initialize a max_seen_so_far variable to the first element of the row. Then for the second element onward, compare the element to max_seen_so_far and replace max_seen_so_far with the element if the element is larger. You can keep track of where that maximum occurred.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by