How to get the index of maximum value in each row of a matrix?
    11 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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.
0 Commenti
Risposte (3)
  KSSV
      
      
 il 18 Apr 2017
        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.
0 Commenti
  Fahim MUMAND
 il 15 Ott 2019
        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 Commenti
  Stephen23
      
      
 il 15 Ott 2019
				>> 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
Vedere anche
Categorie
				Scopri di più su Data Import and Analysis in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





