Remove duplicate values, but keep the initial

4 ビュー (過去 30 日間)
James Boyle
James Boyle 2017 年 2 月 28 日
コメント済み: James Boyle 2017 年 3 月 1 日
For example, i have
x = [0 0 0 1 1 0 0 0 1 0 1 0]
I want the index of the first zero or one, but not the duplicates.
So the vector should become:
0 1 0 1 0 1 0
Where the indices would be '1 4 6 9 10 11 12'
Thanks

採用された回答

Stephen23
Stephen23 2017 年 2 月 28 日
編集済み: Stephen23 2017 年 2 月 28 日
>> x = [0,0,0,1,1,0,0,0,1,0,1,0];
>> idx = [1,1+find(diff(x)~=0)]
idx =
1 4 6 9 10 11 12
>> vec = x(idx)
vec =
0 1 0 1 0 1 0
  1 件のコメント
James Boyle
James Boyle 2017 年 3 月 1 日
Perfect, thank you!

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

その他の回答 (1 件)

Jan
Jan 2017 年 2 月 28 日
x = [0 0 0 1 1 0 0 0 1 0 1 0]
[B, N, Index] = RunLength(x)
Then B contains the unique values and Index the indices.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by