CALCULATION OF MAXIMUM VALUE IN EVERY ROW
Mostra commenti meno recenti
suppose there is a matrix of 3x3 size i want to calculate the maximum value in every row.
ex: 3 4 5
6 9 0
1 2 3
the result i should get at the end should be like this
0 0 1
0 1 0
0 0 1
[EDITED, code formatted, Jan]
2 Commenti
Brahima DRABO
il 16 Gen 2016
suppose your matrix is A: result = A==max(A,[],2)
Stephen23
il 16 Gen 2016
@Brahima DRABO: you should put this as an answer too: you would get votes.
Note to others: ignore the accepted answer, it is very poor code.
Risposta accettata
Più risposte (3)
Jonathan Sullivan
il 7 Mar 2012
m = [ 3 4 5
6 9 0
1 2 3];
mx = max(m,[],2); % EDIT: was max(m,2); Oops
out = bsxfun(@eq,m,mx);
1 Commento
raj
il 7 Mar 2012
Jan
il 6 Mar 2012
[v, sub] = max(a, [], 2); % [EDITED], was "max(a, 2)"
sa = size(a);
b = zeros(sa);
index = sub2ind(sa, 1:sa(1), sub);
b(index) = 1;
I cannot try it currently and I find the documentation of sub2ind confusing in this point: Perhaps the 2nd and 3rd argument must be swapped: sub2ind(sa, sub, 1:sa(1)).
raj
il 7 Mar 2012
0 voti
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!