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 common rows and columns from two matrices.

Subject: How to take common rows and columns from two matrices.

From: jewi

Date: 10 Jun, 2011 11:50:23

Message: 1 of 5

Hello all,

Could anyone help plz;
I have two matrices with different number of columns and rows.
e.g,
     A =
     1 6 11 16
     2 7 12 17
     3 8 13 18
     4 9 14 19
     5 10 15 20
and
     B=
      2 7
      5 10

I want to select rows from A which are common with B, but i want to take the whole row from A with all columns.
For example in the above case my desired result would be;
   C=
   2 7 12 17
   5 10 15 20

Thanks,
Jewi.

Subject: How to take common rows and columns from two matrices.

From: Shawn Bonneau

Date: 10 Jun, 2011 13:16:10

Message: 2 of 5

The code that follows uses logical indexing and the colon operator, and
assumes that matrix A is larger than B.


A = [1 6 11 16
    2 7 12 17
    3 8 13 18
    4 9 14 19
    5 10 15 20];

B=[ 2 7
    5 10];

for rowA = 1:size(A,1) %loop through all rows in matrix A

    for rowB = 1:size(B,1) %for each A row, loop through all B rows
      rowAmatchesRowB(rowB)= (all(A(rowA,1:size(B,2))==B(rowB,:)));
%check if the elements in row A (up to the number in row B) match with all
those in row B
    end

    AIndex(rowA)=any(rowAmatchesRowB); %if row A matched any row in B,
set the index to true

end

C=A(AIndex,:) %use logical indexing to get desired rows



"jewi " <enphy@hotmail.com> wrote in message
news:ist0dv$our$1@newscl01ah.mathworks.com...
> Hello all,
>
> Could anyone help plz;
> I have two matrices with different number of columns and rows.
> e.g, A =
> 1 6 11 16
> 2 7 12 17
> 3 8 13 18
> 4 9 14 19
> 5 10 15 20
> and
> B=
> 2 7
> 5 10
>
> I want to select rows from A which are common with B, but i want to take
> the whole row from A with all columns.
> For example in the above case my desired result would be;
> C=
> 2 7 12 17
> 5 10 15 20
>
> Thanks,
> Jewi.

Subject: How to take common rows and columns from two matrices.

From: Roger Stafford

Date: 11 Jun, 2011 03:33:04

Message: 3 of 5

"jewi" wrote in message <ist0dv$our$1@newscl01ah.mathworks.com>...
> Hello all,
>
> Could anyone help plz;
> I have two matrices with different number of columns and rows.
> e.g,
> A =
> 1 6 11 16
> 2 7 12 17
> 3 8 13 18
> 4 9 14 19
> 5 10 15 20
> and
> B=
> 2 7
> 5 10
>
> I want to select rows from A which are common with B, but i want to take the whole row from A with all columns.
> For example in the above case my desired result would be;
> C=
> 2 7 12 17
> 5 10 15 20
>
> Thanks,
> Jewi.
- - - - - - -
  You can use the 'ismember' function with the 'rows' option to solve this problem in one line:

 C = A(ismember(A(:,1:size(B,2)),B,'rows'),:);

Roger Stafford

Subject: How to take common rows and columns from two matrices.

From: jewi

Date: 13 Jun, 2011 20:30:25

Message: 4 of 5

"Shawn Bonneau" <sbonneau@mathworks.com> wrote in message <ist5eq$9qr$1@newscl01ah.mathworks.com>...
> The code that follows uses logical indexing and the colon operator, and
> assumes that matrix A is larger than B.
>
>
> A = [1 6 11 16
> 2 7 12 17
> 3 8 13 18
> 4 9 14 19
> 5 10 15 20];
>
> B=[ 2 7
> 5 10];
>
> for rowA = 1:size(A,1) %loop through all rows in matrix A
>
> for rowB = 1:size(B,1) %for each A row, loop through all B rows
> rowAmatchesRowB(rowB)= (all(A(rowA,1:size(B,2))==B(rowB,:)));
> %check if the elements in row A (up to the number in row B) match with all
> those in row B
> end
>
> AIndex(rowA)=any(rowAmatchesRowB); %if row A matched any row in B,
> set the index to true
>
> end
>
> C=A(AIndex,:) %use logical indexing to get desired rows
>
>
>
> "jewi " <enphy@hotmail.com> wrote in message
> news:ist0dv$our$1@newscl01ah.mathworks.com...
> > Hello all,
> >
> > Could anyone help plz;
> > I have two matrices with different number of columns and rows.
> > e.g, A =
> > 1 6 11 16
> > 2 7 12 17
> > 3 8 13 18
> > 4 9 14 19
> > 5 10 15 20
> > and
> > B=
> > 2 7
> > 5 10
> >
> > I want to select rows from A which are common with B, but i want to take
> > the whole row from A with all columns.
> > For example in the above case my desired result would be;
> > C=
> > 2 7 12 17
> > 5 10 15 20
> >
> > Thanks,
> > Jewi.

Thank you very much shawn; it worked well.

Subject: How to take common rows and columns from two matrices.

From: jewi

Date: 13 Jun, 2011 20:33:02

Message: 5 of 5

"Roger Stafford" wrote in message <isunlg$t1k$1@newscl01ah.mathworks.com>...
> "jewi" wrote in message <ist0dv$our$1@newscl01ah.mathworks.com>...
> > Hello all,
> >
> > Could anyone help plz;
> > I have two matrices with different number of columns and rows.
> > e.g,
> > A =
> > 1 6 11 16
> > 2 7 12 17
> > 3 8 13 18
> > 4 9 14 19
> > 5 10 15 20
> > and
> > B=
> > 2 7
> > 5 10
> >
> > I want to select rows from A which are common with B, but i want to take the whole row from A with all columns.
> > For example in the above case my desired result would be;
> > C=
> > 2 7 12 17
> > 5 10 15 20
> >
> > Thanks,
> > Jewi.
> - - - - - - -
> You can use the 'ismember' function with the 'rows' option to solve this problem in one line:
>
> C = A(ismember(A(:,1:size(B,2)),B,'rows'),:);
>
> Roger Stafford

wow, its awesome. I love it. Just one line and worked perfectly. Thank you very much Roger.

Tags for this Thread

No tags are associated with 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