Effacer les filtres
Effacer les filtres

Extract the Maximum Column elements of every Row

2 vues (au cours des 30 derniers jours)
Pankaja Tanjore
Pankaja Tanjore le 19 Déc 2015
Hello , I have an array
A = [8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
I want to extract maximum column elements for every Row.
In the above array A i have
3 1 ; 3 5 ; 3 6
I want only 3 6 to be retained as that is the maximum column for that row.
Similarly i have 8 1 ;8 4; 8 6 I want only 8 6 as that has the maximum column value.
Final output I want for the array A is as follows
ans =
1 4;
2 7;
3 6;
8 6;
Looking forward to hear from you at the earliest
Thanks
Pankaja

Réponse acceptée

Guillaume
Guillaume le 19 Déc 2015
Your wording is rather poor. It looks like what you want to do is for each unique value in the first column find the corresponding maximum in the second column. This is trivial to obtain with unique and accumarray:
A = [8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
[val, ~, subs] = unique(A(:, 1));
B = [val, accumarray(subs, A(:, 2), [], @max)]

Plus de réponses (0)

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!

Translated by