How to get rid of repeating values inside an array

3 visualizaciones (últimos 30 días)
Rokki
Rokki el 19 de Sept. de 2017
Editada: Andrei Bobrov el 21 de Sept. de 2017
I have a matrix
a=[1 2 3 3 4 4 5];
I want to get rid of values 3 and 4 as they are repeating so that the output becomes
b=[1 2 5]

Respuesta aceptada

José-Luis
José-Luis el 19 de Sept. de 2017
Editada: José-Luis el 19 de Sept. de 2017
b = a(sum(bsxfun(@eq,a,a'))==1)
  2 comentarios
Rokki
Rokki el 19 de Sept. de 2017
Thank you very much
José-Luis
José-Luis el 19 de Sept. de 2017
Editada: José-Luis el 19 de Sept. de 2017
My pleasure.
Please keep in mind that this is inefficient for large arrays though. Just using unique() should take you where you need to go.

Iniciar sesión para comentar.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 19 de Sept. de 2017
Editada: Andrei Bobrov el 21 de Sept. de 2017
v = unique(a);
b = v(histcounts(a,[v(:);v(end)+eps]) == 1);
or
v = unique(a);
b = v(histc(a,v) == 1);
or
aa = sort(a);
t = diff(aa);
b = aa([1 t] & [t 1]);
  3 comentarios
José-Luis
José-Luis el 19 de Sept. de 2017
histcounts() was introduced with R2014b. You don't need it though. unique() is enough. Andrei was giving you two alternatives.

Iniciar sesión para comentar.

Categorías

Más información sobre NaNs 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