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.

Answers (2)

A = [0 1 0 1 1 0 0 0 0 0 1 1 1 0 1]
A(strfind(A,[1,1]))=[]

2 Comments

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]))=[]

This question is closed.

Asked:

on 27 Nov 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!