Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Grouping repeated rows
Date: Wed, 13 Oct 2010 08:10:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 43
Message-ID: <i93pgu$t04$1@fred.mathworks.com>
References: <i93evd$9nu$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1286957406 29700 172.30.248.38 (13 Oct 2010 08:10:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 13 Oct 2010 08:10:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:677991

"ian johnston" <iano1213@gmail.com> wrote in message <i93evd$9nu$1@fred.mathworks.com>...
> Good day Matlab Community,
> 
> I have 2 problems I'd like some help with please:
> 
> Matrix A and B is given to me:
> 
> A = 
>         [      2     1
>                3     1
>                3     2
>                1     3
>                1     2
>                2     3 ]
> 
> B = [ 10   42
>         38   67
>         22   88
>          5    90
>         29   45
>          3    11]
> 
> The first problem I'm  faced with  is firstly concerned with matrix A. How can A be grouped where elements in rows are repeated? So the desired result would be is:
> 
> Grouped_A =  [  2    1    1   2;
>                        3    1    1   3;
>                        3    2    2   3]
> 
> The second problem is trying to use the 'Grouped_A' matrix to also group its corresponding rows in matrix B. So the result I'm trying to get is:
> 
> Grouped_B =  [   10    42    29   45;
>                        38    67    5   90;
>                        22    88    3   11]
> 
> best,
> Ian
- - - - - - - - - - -
[~,~,n] = unique(sort(A,2),'rows');
[~,n] = sort(n);
GA = [A(n(1:2:end),:),A(n(2:2:end),:)];
GB = [B(n(1:2:end),:),B(n(2:2:end),:)];

Roger Stafford