Find index of same values in array

48 vues (au cours des 30 derniers jours)
roberto verrelli
roberto verrelli le 28 Jan 2014
Hi all,
i have to find the index of the same value in an array,see the following example
a=[1 2 3 1]
i want b=[1 4] as output..how can i do this? A solution using find is this
  • u=unique(a)
  • n=histc(a,u)
  • find(a==u(n>1))
but if in the a array there isn't 2 or more same value i get this error
??? Error using ==> eq Matrix dimensions must agree.
Thank you for your support :-)
  1 commentaire
Walter Roberson
Walter Roberson le 28 Jan 2014
What if there are multiple repeated values?

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 28 Jan 2014
Modifié(e) : Andrei Bobrov le 29 Jan 2014
find(ismember(a,u(n>1)))
ADD
a=[nan nan 12 3 5 9 8 7 12 NaN NaN 0 0 2 1 4 4 4]
u=unique(a(~isnan(a)));
n=histc(a,u);
d = u(n > 1);
out =find(ismember(a,d(end)))
  5 commentaires
Andrei Bobrov
Andrei Bobrov le 29 Jan 2014
corrected
roberto verrelli
roberto verrelli le 29 Jan 2014
Now it's perfect!Thank you very much Andrei ;-)

Connectez-vous pour commenter.

Plus de réponses (1)

Pramod Kumar Jangir
Pramod Kumar Jangir le 20 Oct 2019
Hi Andrei,
your code only wokrs for the consecutive equal values. Could you please modify it for random same values in an array?
Pramod

Catégories

En savoir plus sur Creating and Concatenating Matrices 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