How to delete elements that have repeats in arrays
Mostrar comentarios más antiguos
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 comentario
Mickey
el 12 de Ag. de 2011
Respuestas (4)
Oleg Komarov
el 12 de Ag. de 2011
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 comentario
Andrei Bobrov
el 13 de Ag. de 2011
[uv, ~ ,ui] = unique(v,'rows')
out = uv(histc(ui,1:max(ui))==1,:)
Walter Roberson
el 12 de Ag. de 2011
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 comentario
Fangjun Jiang
el 12 de Ag. de 2011
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
el 12 de Ag. de 2011
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 comentario
Oleg Komarov
el 12 de Ag. de 2011
I would suggest to use histc since hist wraps around it.
Mickey
el 12 de Ag. de 2011
Categorías
Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!