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 件のコメント

Walter Roberson
Walter Roberson 2014 年 1 月 28 日
What if there are multiple repeated values?

サインインしてコメントする。

 採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 1 月 28 日
編集済み: Andrei Bobrov 2014 年 1 月 29 日

3 投票

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 件のコメント

roberto verrelli
roberto verrelli 2014 年 1 月 28 日
編集済み: roberto verrelli 2014 年 1 月 28 日
Thank you very much Andrei,it works!! But now i have the problem issued by Walter.. if i have
  • a=[10 9 9 10]
i have to take only the indexes of the higher value in a,in this example 10 is the max value so the output that i would have is b=[1 4] How take care of this?
Andrei Bobrov
Andrei Bobrov 2014 年 1 月 28 日
see ADD part in this answer
roberto verrelli
roberto verrelli 2014 年 1 月 28 日
Thank you Andrei! I have still a problem,sometimes the a matrix is like this
  • a=[12 12 NaN NaN]
in this case n=histc(a,u) returns n=[0 0 0] so it's not working..i can replace NaN with zeros but if then i have the a matrix like this
  • a=[12 0 0 0] it's not working beacuse out =find(ismember(a,d(end))) will be
  • out=[2 3 4]
and not
  • out=[1]
Andrei Bobrov
Andrei Bobrov 2014 年 1 月 29 日
corrected
roberto verrelli
roberto verrelli 2014 年 1 月 29 日
Now it's perfect!Thank you very much Andrei ;-)

サインインしてコメントする。

その他の回答 (1 件)

Pramod Kumar Jangir
Pramod Kumar Jangir 2019 年 10 月 20 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by