Check for identical maximum values in an array
Afficher commentaires plus anciens
Hi guys, I have a matrix, A = [2 4 1;8 3 2;8 8 6;4 5 3] As seen, the above matrix has more than one maximum values. How do i write an if statement testing for multiple maximum values? That is: if (code) % if more than one maximum value exists; ...; % do ... end
Réponse acceptée
Plus de réponses (3)
Star Strider
le 6 Sep 2017
Probably the easiest way is to do logical indexing and count the number of non-zero elements to get the number of maximum values:
A = [2 4 1;8 3 2;8 8 6;4 5 3];
nr_maxvals = nnz(A == max(A(:)));
nr_maxvals =
3
3 commentaires
Jane Ak
le 6 Sep 2017
Walter Roberson
le 6 Sep 2017
if nnz(A == max(A(:))) > 1
...
end
Star Strider
le 6 Sep 2017
@Walter — Thank you.
Number of maximum values:
numMax=sum(A(:) == max(A(:)));
if numMax > 1
%Do your thing
end
2 commentaires
Stephen23
le 6 Sep 2017
@Jane Ak: Star Strider already explained that in their answer from three hours ago.
Jane Ak
le 6 Sep 2017
0 votes
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!