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:
Grouping repeated rows

Subject: Grouping repeated rows

From: ian johnston

Date: 13 Oct, 2010 05:10:05

Message: 1 of 2

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

Subject: Grouping repeated rows

From: Roger Stafford

Date: 13 Oct, 2010 08:10:06

Message: 2 of 2

"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

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