To find the position of the elements which are same in vector

3 ビュー (過去 30 日間)
UTA
UTA 2012 年 9 月 12 日
I have a vector like this: a = [ 2 3 4 2 5 4 3 3]; and I want to find all the index of the elements which has the same value. output like this: # 2, [1 4] # 3, [2 7 8] # 4, [3 6] # 5, [5]

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 12 日
find(a==2)
  2 件のコメント
UTA
UTA 2012 年 9 月 12 日
but I have to search for a(1) to a(end), and that has duplicate ,how to simply this?
Matt Fig
Matt Fig 2012 年 9 月 12 日
編集済み: Matt Fig 2012 年 9 月 12 日
Oh, well that's another thing.
a = [ 2 3 4 2 5 4 3 3];
[J,J] = histc(a,unique(a));
T = accumarray(J.',(1:length(J)).',[], @(x) {x});
Now T has the indices you seek.

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

その他の回答 (2 件)

per isakson
per isakson 2012 年 9 月 12 日
編集済み: per isakson 2012 年 9 月 12 日
Try
num = [ 2 3 4 2 5 4 3 3 ];
unq = unique( num );
ii = arrayfun( @(x) find( num==x ), unq, 'uni', false ) ;
>> unq
unq =
2 3 4 5
>> ii{:}
ans =
1 4
ans =
2 7 8
ans =
3 6
ans =
5
or use a for-loop

Javier
Javier 2013 年 8 月 28 日
編集済み: Javier 2013 年 8 月 28 日
ind=unique(a)
for i = 1 : length(ind);
[z] =find(a==ind(i))
limit(i,:)=[z(1) z(end)]
end
out = [ind limit]
regards

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by