Eliminate duplicates of nonzero elements but keep zeros

I have a logical array. I want to eliminate the duplicates of the 1s but keep all the zeros intact.
Here's an example of what I want.
A = [0 1 0 1 1 0 0 0 0 0 1 1 1 0 1]
new matrix = [0 1 0 1 0 0 0 0 0 1 0 1].
The zeros are the same but all the duplicate 1s after the first are eliminated.

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 27일
A = [0 1 0 1 1 0 0 0 0 0 1 1 1 0 1]
A(strfind(A,[1,1]))=[]

댓글 수: 2

Hi Azzi, I'd like to put it into another matrix. How do I go about doing that? Thank you for the answer.
A = [0 1 0 1 1 0 0 0 0 0 1 1 1 0 1];
B=A;
B(strfind(B,[1,1]))=[]
Andrei Bobrov
Andrei Bobrov 2013년 11월 27일
A(~([false,diff(A)] == 0 & A == 1))

이 질문은 마감되었습니다.

질문:

2013년 11월 27일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by