How to delete elements that have repeats in arrays

Hey, i'm working on a problem where i have to check an array and delete all the elements that appear more than once in it. This is not what the unique function does(it leaves one copy of the repeated element) and i'm not sure if there's a function that does this, any ideas?

댓글 수: 1

Mickey
Mickey 2011년 8월 12일
sorry i should have given an example:
input=
2,3,4,4,5,6,2,2
output=
3,5,6

댓글을 달려면 로그인하십시오.

답변 (4개)

Oleg Komarov
Oleg Komarov 2011년 8월 12일

2 개 추천

Your original question is actually different from what you're trying to accomplish.
To remove duplicate rows:
[unV,locA,locB] = unique(v,'rows');
v(locA(histc(locB,1:size(unV,1)) == 1),:)

댓글 수: 1

[uv, ~ ,ui] = unique(v,'rows')
out = uv(histc(ui,1:max(ui))==1,:)

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 8월 12일

1 개 추천

For single row,
input= [2,3,4,4,5,6,2,2];
[b, i1] = unique(input,'first');
[b, i2] = unique(input,'last');
b(i1==i2);
For multi-rows, if it is unique rows you want to identify,
input = [2,3,4,5
7,3,7,8
2,4,2,1
2,3,4,5
5,6,3,2
2,3,4,5];
[b, i1] = unique(input, 'rows', 'first');
[b, i2] = unique(input, 'rows', 'last');
b(i1==i2,:)

댓글 수: 1

That's a good one, Walter! I was thinking that it could be solved using unique() and setdiff() or setxor() but couldn't figure it out.

댓글을 달려면 로그인하십시오.

Friedrich
Friedrich 2011년 8월 12일

0 개 추천

Hi,
try this
input= [2,3,4,4,5,6,2,2];
un_in = unique(input);
n = hist(input,numel(un_in));
oputput = un_in(n==1)

댓글 수: 1

I would suggest to use histc since hist wraps around it.

댓글을 달려면 로그인하십시오.

Mickey
Mickey 2011년 8월 12일

0 개 추천

thanks, i should have given my example as a multiple row, multiple column array, but i figured it out anyway. here's what my answer looks like:
clear all
clc
v=[2,3,4,5
7,3,7,8
2,4,2,1
2,3,4,5
5,6,3,2
2,3,4,5];
j=0;
n_rows= size(v,1);% Find number of rows in matrix
for n = 1:n_rows %Check each row to see if is repeated
chk = ismember(v,v(n,:),'rows');% Find rows identical to row n
if sum(chk)>1
j=j+1;
rep(j)=n; %row n has repeats, so store its index
end
end
% Going in reverse order, ie from last row to first, delete repeat rows from array
for tmp=j:-1:1
y=rep(tmp);
v(y,:)=[];
end
v

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

질문:

2011년 8월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by