How to delete a specific number from a vector, if there are duplicates after each other?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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));
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!