How to get rid of repeating values inside an array

1 次查看(过去 30 天)
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]

采纳的回答

José-Luis
José-Luis 2017-9-19
编辑:José-Luis 2017-9-19
b = a(sum(bsxfun(@eq,a,a'))==1)
  2 个评论
José-Luis
José-Luis 2017-9-19
编辑:José-Luis 2017-9-19
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.

请先登录,再进行评论。

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2017-9-19
编辑:Andrei Bobrov 2017-9-21
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 个评论
José-Luis
José-Luis 2017-9-19
histcounts() was introduced with R2014b. You don't need it though. unique() is enough. Andrei was giving you two alternatives.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by