Finding repeating values in an array
Afficher commentaires plus anciens
Hello all,
I have 2 arrays
A = [ 0.3 0.6 1 0.6 0.3]
B = [ 3 2 3 6 11 ]
I need to find the position of same elements in B and then the max value of the elements on the corresponding position in A.
In this case the number 3 is repeated in B on positions 1 and 3 so the corresponding values in A are 0.3 and 1 => max( 0.3 , 1 ) = 1
The end resault should be:
A1 = [ 0.6 1 0.6 0.3 ]
B1 = [ 2 3 6 11 ]
Any help is appreciated
3 commentaires
andrea
le 25 Avr 2020
maybe :
pos_in_b = B == 3 ;
max ( A ( pos_in_b) )
dpb
le 25 Avr 2020
The find part is easy enough, the logic of how to build the A1, B1 vectors from A,B and the lookups escapes me entirely, though...???
Vladimir Kostic
le 25 Avr 2020
Modifié(e) : Vladimir Kostic
le 25 Avr 2020
Réponse acceptée
Plus de réponses (2)
You can use the outputs of the unique function to achieve this.
A = [ 0.3 0.6 1 0.6 0.3];
B = [ 3 2 3 6 11];
[B1,~,ind]=unique(B);
A1=accumarray([ones(numel(A),1) ind],A(:),[],@max);
A1
B1
2 commentaires
Vladimir Kostic
le 25 Avr 2020
Rik
le 27 Avr 2020
A bit late, but here you go, no loops required.
andrea
le 25 Avr 2020
0 votes
maybe i do not understand the problem but anyway
[val, ind] = min ( A ( pos_in_b) )
A(ind) = []
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!