A compact way to find max in one column with a condition on the second column
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Given
a = [35 -1
21 1
11 2];
I want the max in the first column, but the max cannot be that one where there is a "-1" in the second column.
Therefore the correct answer is not "35" (since in the second column there is "-1"), but "21".
Do you know a compact way to get this max in the first column, with the condition on the second column ?
0 commentaires
Réponse acceptée
Bruno Luong
le 7 Oct 2022
a = [35 -1
21 1
11 2];
max(a(a(:,2) ~= -1,1))
3 commentaires
Bruno Luong
le 7 Oct 2022
If you want to get row index of the max value:
a = [35 -1
21 1
11 2];
filteridx = find(a(:,2) ~= -1);
[maxvalue, maxidx] = max(a(filteridx,1));
maxidx = filteridx(maxidx)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!