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:
unique rows of a matrix problem

Subject: unique rows of a matrix problem

From: Ioannis Mitsos

Date: 12 Jan, 2010 17:34:04

Message: 1 of 8

Hello,

Lets say that I have a matrix:

N=[ 1 2 3 4 5
      1 2 3 4 5
      1 2 3 4 5
      3 4 6 8 0
      0 0 0 1 0
      1 1 1 3 4]

and I want to create a matrix [A] which will have only unique row vectors of N (exclude duplicate vectors & leave only one of them).

e.g. A=[
                          1 2 3 4 5
                          2 3 4 5 6
                          0 0 0 1 0
                          1 1 1 3 4]

With the help of ImageAnalyst using the following:

[q,i,j]=unique(NN,'rows');
 [h,b]=histc(j,.5:size(NN,1)-.5);
 idx=ismember(j,find(h>1));
 NN(idx,:)=[]

managed to get the matrix: A=[2 3 4 5 6
                                        0 0 0 1 0
                                         1 1 1 3 4]

BUT this matrix does not include the one of the duplicates that I want. In this matrix all the duplicates are excluded.

thanks.

Subject: unique rows of a matrix problem

From: Nathan

Date: 12 Jan, 2010 17:42:12

Message: 2 of 8

On Jan 12, 9:34 am, "Ioannis Mitsos" <ioannis-mit...@hotmail.com>
wrote:
> Hello,
>
> Lets say that I have a matrix:
>
> N=[ 1 2 3 4 5
>       1 2 3 4 5
>       1 2 3 4 5
>       3 4 6 8 0
>       0 0 0 1 0
>       1 1 1 3 4]
>
> and I want to create a matrix [A] which will have only unique row vectors of N (exclude duplicate vectors & leave only one of them).
>
> e.g.                A=[
>                           1 2 3 4 5
>                           2 3 4 5 6
>                           0 0 0 1 0
>                           1 1 1 3 4]
>
> With the help of ImageAnalyst using the following:
>
> [q,i,j]=unique(NN,'rows');
>  [h,b]=histc(j,.5:size(NN,1)-.5);
>  idx=ismember(j,find(h>1));
>  NN(idx,:)=[]
>
> managed to get the matrix: A=[2 3 4 5 6
>                                         0 0 0 1 0
>                                          1 1 1 3 4]
>
> BUT this matrix does not include the one of the duplicates that I want. In this matrix all the duplicates are excluded.
>
> thanks.

N=[ 1 2 3 4 5
      1 2 3 4 5
      1 2 3 4 5
      3 4 6 8 0
      0 0 0 1 0
      1 1 1 3 4] ;
[q i j] = unique(N,'rows');
NN = N(sort(i),:);
%%%%%%%%%%%%%%%%%%%
NN =
     1 2 3 4 5
     3 4 6 8 0
     0 0 0 1 0
     1 1 1 3 4

-Nathan

Subject: unique rows of a matrix problem

From: Loren Shure

Date: 12 Jan, 2010 17:44:32

Message: 3 of 8

In article <hiibqc$nlu$1@fred.mathworks.com>, ioannis-mitsos@hotmail.com
says...
> Hello,
>
> Lets say that I have a matrix:
>
> N=[ 1 2 3 4 5
> 1 2 3 4 5
> 1 2 3 4 5
> 3 4 6 8 0
> 0 0 0 1 0
> 1 1 1 3 4]
>
> and I want to create a matrix [A] which will have only unique row vectors of N (exclude duplicate vectors & leave only one of them).
>
> e.g. A=[
> 1 2 3 4 5
> 2 3 4 5 6
> 0 0 0 1 0
> 1 1 1 3 4]
>
> With the help of ImageAnalyst using the following:
>
> [q,i,j]=unique(NN,'rows');
> [h,b]=histc(j,.5:size(NN,1)-.5);
> idx=ismember(j,find(h>1));
> NN(idx,:)=[]
>
> managed to get the matrix: A=[2 3 4 5 6
> 0 0 0 1 0
> 1 1 1 3 4]
>
> BUT this matrix does not include the one of the duplicates that I want. In this matrix all the duplicates are excluded.
>
> thanks.
>

just try
A = unique(N,'rows')

--
Loren
http://blogs.mathworks.com/loren

Subject: unique rows of a matrix problem

From: Andy

Date: 12 Jan, 2010 17:45:06

Message: 4 of 8

"Ioannis Mitsos" <ioannis-mitsos@hotmail.com> wrote in message <hiibqc$nlu$1@fred.mathworks.com>...
> Hello,
>
> Lets say that I have a matrix:
>
> N=[ 1 2 3 4 5
> 1 2 3 4 5
> 1 2 3 4 5
> 3 4 6 8 0
> 0 0 0 1 0
> 1 1 1 3 4]
>
> and I want to create a matrix [A] which will have only unique row vectors of N (exclude duplicate vectors & leave only one of them).
>
> e.g. A=[
> 1 2 3 4 5
> 2 3 4 5 6
> 0 0 0 1 0
> 1 1 1 3 4]
>
> With the help of ImageAnalyst using the following:
>
> [q,i,j]=unique(NN,'rows');
> [h,b]=histc(j,.5:size(NN,1)-.5);
> idx=ismember(j,find(h>1));
> NN(idx,:)=[]
>
> managed to get the matrix: A=[2 3 4 5 6
> 0 0 0 1 0
> 1 1 1 3 4]
>
> BUT this matrix does not include the one of the duplicates that I want. In this matrix all the duplicates are excluded.
>
> thanks.

I don't understand your last sentence. Do you want to exclude all duplicates, or are you trying to keep a certain one? To exclude all duplicates, simply:

B=unique(N,'rows');

Subject: unique rows of a matrix problem

From: Wayne King

Date: 12 Jan, 2010 17:47:04

Message: 5 of 8

"Ioannis Mitsos" <ioannis-mitsos@hotmail.com> wrote in message <hiibqc$nlu$1@fred.mathworks.com>...
> Hello,
>
> Lets say that I have a matrix:
>
> N=[ 1 2 3 4 5
> 1 2 3 4 5
> 1 2 3 4 5
> 3 4 6 8 0
> 0 0 0 1 0
> 1 1 1 3 4]
>
> and I want to create a matrix [A] which will have only unique row vectors of N (exclude duplicate vectors & leave only one of them).
>
> e.g. A=[
> 1 2 3 4 5
> 2 3 4 5 6
> 0 0 0 1 0
> 1 1 1 3 4]
>
> With the help of ImageAnalyst using the following:
>
> [q,i,j]=unique(NN,'rows');
> [h,b]=histc(j,.5:size(NN,1)-.5);
> idx=ismember(j,find(h>1));
> NN(idx,:)=[]
>
> managed to get the matrix: A=[2 3 4 5 6
> 0 0 0 1 0
> 1 1 1 3 4]
>
> BUT this matrix does not include the one of the duplicates that I want. In this matrix all the duplicates are excluded.
>
> thanks.

Hi, Why does

B = unique(N,'rows');

not work?

N = [1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     3 4 6 8 0
     0 0 0 1 0
     1 1 1 3 4];

 B = unique(N,'rows');
% returns the following
B =

     0 0 0 1 0
     1 1 1 3 4
     1 2 3 4 5
     3 4 6 8 0


Wayne

Subject: unique rows of a matrix problem

From: Ioannis Mitsos

Date: 12 Jan, 2010 18:01:08

Message: 6 of 8

Nathan <ngreco32@gmail.com> wrote in message <5cc68400-c32e-49ce-a10f-e60e466981a7@m25g2000yqc.googlegroups.com>...
> On Jan 12, 9:34 am, "Ioannis Mitsos" <ioannis-mit...@hotmail.com>
> wrote:
> > Hello,
> >
> > Lets say that I have a matrix:
> >
> > N=[ 1 2 3 4 5
> >       1 2 3 4 5
> >       1 2 3 4 5
> >       3 4 6 8 0
> >       0 0 0 1 0
> >       1 1 1 3 4]
> >
> > and I want to create a matrix [A] which will have only unique row vectors of N (exclude duplicate vectors & leave only one of them).
> >
> > e.g.                A=[
> >                           1 2 3 4 5
> >                           2 3 4 5 6
> >                           0 0 0 1 0
> >                           1 1 1 3 4]
> >
> > With the help of ImageAnalyst using the following:
> >
> > [q,i,j]=unique(NN,'rows');
> >  [h,b]=histc(j,.5:size(NN,1)-.5);
> >  idx=ismember(j,find(h>1));
> >  NN(idx,:)=[]
> >
> > managed to get the matrix: A=[2 3 4 5 6
> >                                         0 0 0 1 0
> >                                          1 1 1 3 4]
> >
> > BUT this matrix does not include the one of the duplicates that I want. In this matrix all the duplicates are excluded.
> >
> > thanks.
>
> N=[ 1 2 3 4 5
> 1 2 3 4 5
> 1 2 3 4 5
> 3 4 6 8 0
> 0 0 0 1 0
> 1 1 1 3 4] ;
> [q i j] = unique(N,'rows');
> NN = N(sort(i),:);
> %%%%%%%%%%%%%%%%%%%
> NN =
> 1 2 3 4 5
> 3 4 6 8 0
> 0 0 0 1 0
> 1 1 1 3 4
>
> -Nathan

Very fast and correct!!Thanks a lot Nathan this is he answer to my problem..

Subject: unique rows of a matrix problem

From: safae safae

Date: 13 Dec, 2011 11:45:10

Message: 7 of 8

Hi I have another problem

let's consider
N=[ 1 2 3 4 5
      1 2 3 4 5
      0 0 0 1 0
      3 4 6 8 0
      1 2 3 4 5
      1 1 1 3 4]
I just permute row 3 with row 5 and I get running
[q i j] = unique(N,'rows')
 NN = N(sort(i),:)

NN =

     0 0 0 1 0
     3 4 6 8 0
     1 2 3 4 5
     1 1 1 3 4

but I want to get

NN =

      1 2 3 4 5
     3 4 6 8 0
     0 0 0 1 0
     1 1 1 3 4

in this order, how can I do?

Subject: unique rows of a matrix problem

From: safae safae

Date: 13 Dec, 2011 12:26:08

Message: 8 of 8

"safae safae" wrote in message <jc7ds6$j88$1@newscl01ah.mathworks.com>...
> Hi I have another problem
>
> let's consider
> N=[ 1 2 3 4 5
> 1 2 3 4 5
> 0 0 0 1 0
> 3 4 6 8 0
> 1 2 3 4 5
> 1 1 1 3 4]
> I just permute row 3 with row 5 and I get running
> [q i j] = unique(N,'rows')
> NN = N(sort(i),:)
>
> NN =
>
> 0 0 0 1 0
> 3 4 6 8 0
> 1 2 3 4 5
> 1 1 1 3 4
>
> but I want to get
>
> NN =
>
> 1 2 3 4 5
> 3 4 6 8 0
> 0 0 0 1 0
> 1 1 1 3 4
>
> in this order, how can I do?


well I find this solution

[d,~,J]=unique(N,'rows');
    [b,order]=unique(J,'first');
    [~,index]=sort(order);
    b=b(index);
    Nsorted=d(b,:)

and I get the result.

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