How to get rid of repeating values inside an array

조회 수: 1 (최근 30일)
Rokki
Rokki 2017년 9월 19일
편집: Andrei Bobrov 2017년 9월 21일
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
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일
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.
Andrei Bobrov
Andrei Bobrov 2017년 9월 19일
fixed

댓글을 달려면 로그인하십시오.

카테고리

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