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:
help with unique function

Subject: help with unique function

From: Fraser Dickson

Date: 5 May, 2010 10:13:04

Message: 1 of 8

Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.

I have a 2d array always with 2 cols

I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1

eg.

a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]

i want b to equal what i have mentioned above which should hopefully give me

b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]

a = unique(b) does not do this ?

Can someone help me out ?

Subject: help with unique function

From: Wayne King

Date: 5 May, 2010 10:38:04

Message: 2 of 8

"Fraser Dickson" <fraser.dickson@gmail.com> wrote in message <hrrgbg$mk0$1@fred.mathworks.com>...
> Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.
>
> I have a 2d array always with 2 cols
>
> I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1
>
> eg.
>
> a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]
>
> i want b to equal what i have mentioned above which should hopefully give me
>
> b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]
>
> a = unique(b) does not do this ?
>
> Can someone help me out ?

Hi, One way

[elements,I,J] = unique(a(:,2),'first');
B = zeros(size(a));
B(I,:)=a(I,:);
EmptyInd = setdiff(1:size(a,1),I);
B(EmptyInd,:)=[];

Wayne

Subject: help with unique function

From: Roger Stafford

Date: 5 May, 2010 17:39:05

Message: 3 of 8

"Fraser Dickson" <fraser.dickson@gmail.com> wrote in message <hrrgbg$mk0$1@fred.mathworks.com>...
> Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.
>
> I have a 2d array always with 2 cols
>
> I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1
>
> eg.
>
> a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]
>
> i want b to equal what i have mentioned above which should hopefully give me
>
> b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]
>
> a = unique(b) does not do this ?
>
> Can someone help me out ?
- - - - - - - -
  It depends on what you want, Fraser. The unique function puts the unique elements in ascending order. We can't tell from your example if this is actually what you want because the first occurrence of numbers in the second column there happens to already be in ascending order.

  If you want to leave them in ascending order do this

 [t,m,n] = unique(a(:,2),'first');
 b = a(m,:);

  On the other hand if you want to preserve the original order that was present in a, do this

 [t,m,n] = unique(a(:,2),'first');
 [t,p] = sort(m);
 b = a(m(p),:);

Roger Stafford

Subject: help with unique function

From: us

Date: 5 May, 2010 17:53:04

Message: 4 of 8

"Fraser Dickson" <fraser.dickson@gmail.com> wrote in message <hrrgbg$mk0$1@fred.mathworks.com>...
> Hi im trying to use the uniqie function to get rid of duplicate entries within my array but im having some trouble with it.
>
> I have a 2d array always with 2 cols
>
> I want to only perofrm the unique function to col2 but always delete the corresponding value in that row form col 1
>
> eg.
>
> a = [ 2,23 ; 3,25; 4,23; 5,25; 6,26 ;7,23 ; 8,27 ; 9,28 ; 10,29]
>
> i want b to equal what i have mentioned above which should hopefully give me
>
> b = [2,23 ; 3,25 ; 6,26 ; 8,27 ; 9,28 ; 10,29]
>
> a = unique(b) does not do this ?
>
> Can someone help me out ?

one of the many other solutions

     a=[
          2 23
          3 25
          4 23
          5 25
          6 26
          7 23
          8 27
          9 28
         10 29
     ];
     [ax,ax,ax]=unique(a(:,2));
     [ax,ax]=unique(ax,'first');
     r=a(ax,:)
%{
     2 23
     3 25
     6 26
     8 27
     9 28
    10 29
%}

us

Subject: help with unique function

From: Bruno Luong

Date: 5 May, 2010 18:31:22

Message: 5 of 8

"us " <us@neurol.unizh.ch> wrote in message <hrsba0$bj3$1@fred.mathworks.com>...
>

> [ax,ax,ax]=unique(a(:,2));
> [ax,ax]=unique(ax,'first');
> r=a(ax,:)

Hm, Unless if I'm mistaken I can't see why the advantage above to this:

 [ax,ax]=unique(a(:,2),'first');
 r=a(ax,:)

otherwise than heating twice the CPU and and being really obscure. Can we be enlighten us? I admittedly staring the three lines of code for a moment... LOL

Bruno

Subject: help with unique function

From: us

Date: 5 May, 2010 18:42:04

Message: 6 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hrsdhq$b4u$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hrsba0$bj3$1@fred.mathworks.com>...
> >
>
> > [ax,ax,ax]=unique(a(:,2));
> > [ax,ax]=unique(ax,'first');
> > r=a(ax,:)
>
> Hm, Unless if I'm mistaken I can't see why the advantage above to this:
>
> [ax,ax]=unique(a(:,2),'first');
> r=a(ax,:)
>
> otherwise than heating twice the CPU and and being really obscure. Can we be enlighten us? I admittedly staring the three lines of code for a moment... LOL
>
> Bruno

bruno
i just LOVE to type the name AX...
even more LOLs...

us
ps: i must have a bad hair-day... really(!)...

Subject: help with unique function

From: Roger Stafford

Date: 5 May, 2010 20:49:05

Message: 7 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hrsdhq$b4u$1@fred.mathworks.com>...
> Hm, Unless if I'm mistaken I can't see why the advantage above to this:
>
> [ax,ax]=unique(a(:,2),'first');
> r=a(ax,:)
> .......

  Hey, Bruno, that's equivalent to what I wrote earlier with

 [t,m,n] = unique(a(:,2),'first');
 b = a(m,:);

(assuming that they didn't need to be reordered back to the original order.)

Roger Stafford

Subject: help with unique function

From: Bruno Luong

Date: 5 May, 2010 21:09:04

Message: 8 of 8

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hrslk1$4kq$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hrsdhq$b4u$1@fred.mathworks.com>...
> > Hm, Unless if I'm mistaken I can't see why the advantage above to this:
> >
> > [ax,ax]=unique(a(:,2),'first');
> > r=a(ax,:)
> > .......
>
> Hey, Bruno, that's equivalent to what I wrote earlier with
>
> [t,m,n] = unique(a(:,2),'first');
> b = a(m,:);

Quite right Roger, this equivalence is clear with 'clear n t'. Ins't clear? LOL

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