# How to get rid of repeating values inside an array

3 views (last 30 days)
Rokki on 19 Sep 2017
Edited: Andrei Bobrov on 21 Sep 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]
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

José-Luis on 19 Sep 2017
Edited: José-Luis on 19 Sep 2017
b = a(sum(bsxfun(@eq,a,a'))==1)
##### 2 CommentsShowHide 1 older comment
José-Luis on 19 Sep 2017
Edited: José-Luis on 19 Sep 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.

Sign in to comment.

### More Answers (1)

Andrei Bobrov on 19 Sep 2017
Edited: Andrei Bobrov on 21 Sep 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 CommentsShowHide 2 older comments
Andrei Bobrov on 19 Sep 2017
fixed

Sign in to comment.

### Categories

Find more on Logical in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by