How can i find the position of same elements in matlab......

1 回表示 (過去 30 日間)
Javier
Javier 2013 年 8 月 28 日
For Example :
x=[1 1 1 2 2 3 3 5 5 5 7 7 7]
result =
1 1 3
2 4 5
3 6 7
5 8 10
7 11 13
Regards.......
I try something like this, ('works')but is really slow
ind=unique(x)
for i = 1 : length(ind);
[z] =find(x==ind(i))
limit(i,:)=[z(1) z(end)]
end

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 8 月 28 日
編集済み: Andrei Bobrov 2013 年 8 月 28 日
[a,b1] = unique(x,'first');
[a,b2] = unique(x,'last');
result = [a(:),b1,b2];
or
ii = [find([true;diff(x(:))~=0]);numel(x)+1];
result=[x(ii(1:end-1))',ii(1:end-1),ii(2:end)-1];
  1 件のコメント
Javier
Javier 2013 年 8 月 30 日
ULTRA SUPER FAST, THANKS ANDREI BOBROV...............

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 8 月 28 日
hint: look at find() in conjunction with diff(x)
  1 件のコメント
Javier
Javier 2013 年 8 月 28 日
編集済み: Javier 2013 年 8 月 30 日
Thanks Walter,

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

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by