removal of doubles (both values)

Hi all,
I have an nx3 matrix of values. The values are sorted and there are duplicates(never more than 1 duplicate).
If duplicates occur, I would like to replace both duplicates with a place holder..ex(0,0,0) ex
1 1 1
2 2 2
2 2 2
3 3 3
4 4 4
4 4 4
ans
1 1 1
0 0 0
0 0 0
3 3 3
0 0 0
0 0 0
Any thoughts would be very helpful, thanks

Answers (2)

I'm sure someone will come up with a one-liner, but here's a three-liner way
data = [1 1 1; 2 2 2; 2 2 2; 3 3 3; 4 4 4; 4 4 4; 4 4 4]
duplicateRows = diff(data(:,1)) == 0
duplicateRows = [duplicateRows;0] | [0; duplicateRows]
data(duplicateRows,:) = 0

1 Comment

This works great! Thank you very much
This is one way to do it.
a=[1 1 1
2 2 2
2 2 2
3 3 3
4 4 4
4 4 4];
[ua,indI,indJ]=unique(a,'rows');
DupRows=setdiff(1:size(a,1),indI);
index=[DupRows,DupRows+1];
a(index,:)=0;

1 Comment

This is also a big help, thanks!

This question is closed.

Tags

Asked:

on 28 Oct 2011

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!