How to delete a specific number from a vector, if there are duplicates after each other?

Hello,
I have a vector with numbers, see example below:
[1 2 3 4 5 0 4 6 5 0 0 98 0 0 1 2 23 4 0 0 0 2 5 8 0]
I would like to delete the second 0 where there are duplicates or more 0 following each other, so the new vector looks like below:
[1 2 3 4 5 0 4 6 5 0 98 0 1 2 23 4 0 2 5 8 0]
How can I do this?
Thank you for your help.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 6 月 19 日
編集済み: Andrei Bobrov 2014 年 6 月 19 日
A = [1 2 3 4 5 0 4 6 5 0 0 98 0 0 1 2 23 4 0 0 0 2 5 8 0];
out = A([true;diff(A(:))~=0]);
or only fo zeros in A
out = A(~([true;diff(A(:))~=0] == 0 & A(:) == 0));

1 件のコメント

Matilda
Matilda 2014 年 6 月 19 日
This solution works perfect. Thank you for your help!

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

その他の回答 (1 件)

ES
ES 2014 年 6 月 19 日
編集済み: ES 2014 年 6 月 19 日
a = [1 2 3 4 5 0 4 6 5 0 0 98 0 0 1 2 23 4 0 0 0 2 5 8 0];
a(a(l)==0 & a(l+1)==0)=[];

カテゴリ

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

質問済み:

2014 年 6 月 19 日

コメント済み:

2014 年 6 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by