Finding repeating values in an array

1 vue (au cours des 30 derniers jours)
Vladimir Kostic
Vladimir Kostic le 25 Avr 2020
Commenté : Rik le 27 Avr 2020
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
dpb
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
Vladimir Kostic le 25 Avr 2020
Modifié(e) : Vladimir Kostic le 25 Avr 2020
dpb
If you look at the elements of A as the numerator and elements of B as the denominator i need to find fractions with the same denominator and use the one with the higher numerator.
In my example i have
0.3 0.6 1 0.6 0.3
3 2 3 6 11
So there are 2 fractions with the denominator of 3 (it isn't predetermined that it is 3 just happened to be this way, it can be any number and I can be more that just 1 number that repeats) and the numerators of those 2 fractions are 0.3 and 1, where the higher number is 1 ( 1>0.3). So i need to erase the fraction with the lower numerator.
0.6 1 0.6 0.3
2 3 6 11
Hope this clears it up

Connectez-vous pour commenter.

Réponse acceptée

aleksa markovic
aleksa markovic le 25 Avr 2020
Modifié(e) : aleksa markovic le 25 Avr 2020
You ca try something like this:
Xa = [3 2 3 6 11];
mua = [.3 .6 1 .6 .3];
tmpX = [];
tmpmu = [];
for i = 1:size(Xa,2)
if(sum(tmpX == Xa(i)) > 0)
tmpmu(tmpX == Xa(i)) = max(mua(i),tmpmu(tmpX == Xa(i)));
else
tmpX = [tmpX Xa(i)];
tmpmu = [tmpmu mua(i)];
end
end
Xa = tmpX;
mua = tmpmu;

Plus de réponses (2)

Rik
Rik le 25 Avr 2020
Modifié(e) : Rik le 27 Avr 2020
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
Vladimir Kostic le 25 Avr 2020
Could you explain further into detail, I'm still pretty new at MATLAB
Rik
Rik le 27 Avr 2020
A bit late, but here you go, no loops required.

Connectez-vous pour commenter.


andrea
andrea le 25 Avr 2020
maybe i do not understand the problem but anyway
[val, ind] = min ( A ( pos_in_b) )
A(ind) = []

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by