필터 지우기
필터 지우기

How to get the index of maximum value in each row of a matrix?

조회 수: 21 (최근 30일)
SUNANNA S S
SUNANNA S S 2017년 4월 18일
댓글: Bryan Chambers 2020년 2월 19일
For eg, Matrix A=[1 2 3;2 0 0; 3 8 5 ]; The result I want to get is:
[p q]=1 3
2 1
3 2
I tried this
[p,q] = max(A,[],2);
but, it's not my desired output. Thanks in advance.

답변 (3개)

Thorsten
Thorsten 2017년 4월 18일
[~, q] = max(A, [], 2) ;
p = (1:size(A, 1))';

KSSV
KSSV 2017년 4월 18일
A=[1 2 3;2 0 0; 3 8 5 ];
[val,idx] = max(A,[],2) ;
[~,j] = ind2sub(size(A(1,:)),idx) ;
i = [1:size(A,1)]' ;
[i j]
There would be more elegant solution than this.

Fahim MUMAND
Fahim MUMAND 2019년 10월 15일
What if
M =
7 8 8 2
1 8 1 7
9 4 10 4
10 7 1 10
7 10 1 1
and I want the indices of maximums (for example there are two in first row)?
  댓글 수: 2
Stephen23
Stephen23 2019년 10월 15일
>> V = max(M,[],2);
>> idx = M==V % logical indices
idx =
0 1 1 0
0 1 0 0
0 0 1 0
1 0 0 1
0 1 0 0
>> [row,col] = find(idx) % subscript indices
row =
4
1
2
5
1
3
4
col =
1
2
2
2
3
3
4

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Signal Generation and Preprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by