How to get rid of repeating values inside an array

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 日

1 投票

b = a(sum(bsxfun(@eq,a,a'))==1)

2 件のコメント

Rokki
Rokki 2017 年 9 月 19 日
Thank you very much
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 日

0 投票

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 件のコメント

Rokki
Rokki 2017 年 9 月 19 日
Thank you for help. I am getting an error
Undefined function 'histcounts' for input arguments of type 'double'.
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.
Andrei Bobrov
Andrei Bobrov 2017 年 9 月 19 日
fixed

サインインしてコメントする。

カテゴリ

質問済み:

2017 年 9 月 19 日

編集済み:

2017 年 9 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by