Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to take common rows and columns from two matrices.
Date: Sat, 11 Jun 2011 03:33:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 31
Message-ID: <isunlg$t1k$1@newscl01ah.mathworks.com>
References: <ist0dv$our$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1307763184 29748 172.30.248.37 (11 Jun 2011 03:33:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 11 Jun 2011 03:33:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:731426

"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