Thread Subject: modified unique

Subject: modified unique

From: gopla

Date: 24 Oct, 2007 12:54:46

Message: 1 of 5

Hi,
     I have a array A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9] and want output B
= [1 2 4 7 8 9]

i.e. select only the unique numbers from the array and discard all
numbers that get repeated in the original array.

I have tried with 'unique' and then a for loop which 'find' number of
occurrence of a element of the output of unique in the original array.
I want to simplify this and try to remove the for loop.

With Regards
Gopla

Subject: modified unique

From: John D'Errico

Date: 24 Oct, 2007 13:02:24

Message: 2 of 5

 gopla <gopalgajjar@gmail.com> wrote in message
<1193230486.076915.167300@z24g2000prh.googlegroups.com>...
> Hi,
> I have a array A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9] and want output B
> = [1 2 4 7 8 9]
>
> i.e. select only the unique numbers from the array and discard all
> numbers that get repeated in the original array.
>
> I have tried with 'unique' and then a for loop which 'find' number of
> occurrence of a element of the output of unique in the original array.
> I want to simplify this and try to remove the for loop.


A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9];

[B,count] = consolidator(A);
B(count>1) = [];

B =
     1
     2
     4
     7
     8
     9

Find consolidator on the file exchange. (Beware
the wrapped URL. Copy both lines into your
browser)

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?
objectId=8354&objectType=file

HTH,
John

Subject: modified unique

From: Nick Denman

Date: 24 Oct, 2007 13:04:49

Message: 3 of 5

Hi Gopla

 gopla <gopalgajjar@gmail.com> wrote in message
<1193230486.076915.167300@z24g2000prh.googlegroups.com>...
> Hi,
> I have a array A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9] and
want output B
> = [1 2 4 7 8 9]
>
> i.e. select only the unique numbers from the array and
discard all
> numbers that get repeated in the original array.
>

You may want to try histc.

> I have tried with 'unique' and then a for loop which
'find' number of
> occurrence of a element of the output of unique in the
original array.
> I want to simplify this and try to remove the for loop.
>

One possible way

A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9];
AA = unique(A);
n = histc(A,AA);
AA(n==1);

> With Regards
> Gopla
>

Cheers, Nick

Subject: modified unique

From: Lee Newman

Date: 24 Oct, 2007 13:05:45

Message: 4 of 5

 gopla <gopalgajjar@gmail.com> wrote in message
<1193230486.076915.167300@z24g2000prh.googlegroups.com>...
> Hi,
> I have a array A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9] and
want output B
> = [1 2 4 7 8 9]
>
> i.e. select only the unique numbers from the array and
discard all
> numbers that get repeated in the original array.
>
> I have tried with 'unique' and then a for loop which
'find' number of
> occurrence of a element of the output of unique in the
original array.
> I want to simplify this and try to remove the for loop.
>
> With Regards
> Gopla
>

Here's one approach that uses the tabulate function (see
help in statistics toolbox):

A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9];
x=tabulate(A);
result=x(x(:,2)==1)'

I suspect there is lower-level/more concise function that
returns frequency counts that might be better than
tabulate(), but this works.



Subject: modified unique

From: Jean-Marc Gulliet

Date: 24 Oct, 2007 13:35:24

Message: 5 of 5

gopla wrote:

> I have a array A = [ 1 2 3 3 4 5 5 5 6 6 7 8 9] and want output B
> = [1 2 4 7 8 9]
>
> i.e. select only the unique numbers from the array and discard all
> numbers that get repeated in the original array.
>
> I have tried with 'unique' and then a for loop which 'find' number of
> occurrence of a element of the output of unique in the original array.
> I want to simplify this and try to remove the for loop.

  One possible way is using *union* (operation on sets).

  >> A = [1 2 3 3 4 5 5 5 6 6 7 8 9];
  >> union(A,A)
  ans =
       1 2 3 4 5 6 7 8 9

  HTH,
  --
  Jean-Marc

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
tabulate Lee Newman 24 Oct, 2007 09:10:24
histc Nick Denman 24 Oct, 2007 09:05:09
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com