Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to take out of repeat element from a array

Subject: How to take out of repeat element from a array

From: Feng

Date: 27 Sep, 2008 04:08:01

Message: 1 of 3

I am wondering if there is a function in matlabe can help me to do this.
For example, I have an array, [1 2 3 4 5 3 4 7]
I want to get a new array that only contain the elements that show up in old array once, meaning get [1 2 5 7].
I know unique function in matlab, but the unique result will give [1 2 3 4 5 7].

Anyone can help me? Thanks

Subject: How to take out of repeat element from a array

From: Matt Fig

Date: 27 Sep, 2008 04:35:03

Message: 2 of 3

One method:

a = floor(rand(1,10)*8) % Guarantee some repeats.
[b,If,Jf] = unique(a,'first');
[b,Il,Jl] = unique(a,'last');
a(intersect(If,Il))

Subject: How to take out of repeat element from a array

From: Bruno Luong

Date: 27 Sep, 2008 07:41:02

Message: 3 of 3

"Matt Fig" <spamanon@yahoo.com> wrote in message <gbkd5n$qol$1@fred.mathworks.com>...
> One method:
>
> a = floor(rand(1,10)*8) % Guarantee some repeats.
> [b,If,Jf] = unique(a,'first');
> [b,Il,Jl] = unique(a,'last');
> a(intersect(If,Il))

In complement to Matt's solution, the last statement can be replaced alternatively by

a(If(If==Il)); % probably faster

%
Here is another solution

a = floor(rand(1,10)*8)

u=unique(a);
u(hist(a,u)==1)

% Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us