Borrar filtros
Borrar filtros

How do you remove duplicates without using unique?

3 visualizaciones (últimos 30 días)
jeff417
jeff417 el 9 de Abr. de 2016
Comentada: Star Strider el 9 de Abr. de 2016
I have a problem where I have to remove duplicates from a column without using unique and without sorting the data. My current code is able to remove duplicates without unique, but the data has to be sorted for it to work. Is there a way to remove the duplicates without sorting and without using unique?
z = length(A);
for i = 1:(z-1)
if A(i+1) == A(i)
A(i) = A(i) - A(i+1);
end
end
A(A==0) = [ ];
end

Respuesta aceptada

Star Strider
Star Strider el 9 de Abr. de 2016
One approach:
A = [1;2;3;3;4;5;6;7;7;8;9];
Ac = zeros(size(A));
for k1 = 1:length(A)-1
Ac(k1) = sum(A(k1) == A(k1+1:end));
end
Out = A(Ac == 0)
Out =
1
2
3
4
5
6
7
8
9
  2 comentarios
jeff417
jeff417 el 9 de Abr. de 2016
Works perfectly, thank you!
Star Strider
Star Strider el 9 de Abr. de 2016
My pleasure!
It was fun to write that!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by