How can I strip duplicates?
Mostrar comentarios más antiguos

I wish to remove all duplicate rows based off of the first column. That is, I want to just strip the excess away. I am struggling to understand how to implement "unique" here. Thanks in advance
5 comentarios
dpb
el 8 de Abr. de 2021
Looks like you probably just want the 'rows' optional parameter...
res=unique(data,'rows'); % keep original, too, or
data=unique(data,'rows'); % throw the original duplicates away
Cris LaPierre
el 8 de Abr. de 2021
Perhaps something got lost as you transferred your answer here, but it looks like both lines of code will produce the same output.
dpb
el 8 de Abr. de 2021
Yes, but as the comments note the second overwrites the data array whereas the first creates a new result variable, keeping the original data as well in case there is some other reason to have it, too, going forward.
Cris LaPierre
el 9 de Abr. de 2021
Ah, got it. Thanks.
per isakson
el 13 de Oct. de 2021
Editada: per isakson
el 13 de Oct. de 2021
Respuestas (1)
Fangjun Jiang
el 9 de Abr. de 2021
If you want
- remove duplicates only based on values in the first column
- Do not want the returned values be sorted
then you need to do this
in=[[5;5;5;4;4;4;3;3;3;1],(1:10)'];
[~, index]=unique(in(:,1),'stable');
out=in(index,:)
Categorías
Más información sobre Shifting and Sorting Matrices 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!