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:
Identifying Rows with elements in different order

Subject: Identifying Rows with elements in different order

From: Aidy

Date: 17 Oct, 2010 05:59:05

Message: 1 of 5

hello friends,

I have 2 matrices , A and B,


 A = [ 1 7 1 4
        2 3 1 4
        2 4 1 4
        2 5 1 4
        2 6 1 4]
 
B = [ 1 1 4 7
               5 2 4 1
               8 5 3 4]


I want to get the intersection of rows where all elements are the same in rows of A and B , even though in different order.

So from A and B, i'd wish to obtain:

A_B =
                      [1 7 1 4;
                      2 5 1 4]

This is so since the corresponding rows in B are ' 1 1 4 7' and ' 5 2 4 1'.

take care
Aiden

Subject: Identifying Rows with elements in different order

From: Nasser M. Abbasi

Date: 17 Oct, 2010 06:19:56

Message: 2 of 5

On 10/16/2010 10:59 PM, Aidy wrote:
> hello friends,
>
> I have 2 matrices , A and B,
>
>
> A = [ 1 7 1 4
> 2 3 1 4
> 2 4 1 4
> 2 5 1 4
> 2 6 1 4]
>
> B = [ 1 1 4 7
> 5 2 4 1
> 8 5 3 4]
>
>
> I want to get the intersection of rows where all elements are the same in rows of A and B , even though in different order.
>
> So from A and B, i'd wish to obtain:
>
> A_B =
> [1 7 1 4;
> 2 5 1 4]
>
> This is so since the corresponding rows in B are ' 1 1 4 7' and ' 5 2 4 1'.
>
> take care
> Aiden

I think the Matlab command intersect(A,B,'rows') :

"c = intersect(A, B, 'rows') when A and B are matrices with the same
number of columns returns the rows common to both A and B."

should really have a 'unique' option to it so that order of elements in
rows do not matter. This is a set theory operation after all?

--Nasser

Subject: Identifying Rows with elements in different order

From: Nasser M. Abbasi

Date: 17 Oct, 2010 06:34:12

Message: 3 of 5

On 10/16/2010 10:59 PM, Aidy wrote:
> hello friends,
>
> I have 2 matrices , A and B,
>
>
> A = [ 1 7 1 4
> 2 3 1 4
> 2 4 1 4
> 2 5 1 4
> 2 6 1 4]
>
> B = [ 1 1 4 7
> 5 2 4 1
> 8 5 3 4]
>
>
> I want to get the intersection of rows where all elements are the same in rows of A and B , even though in different order.
>
> So from A and B, i'd wish to obtain:
>
> A_B =
> [1 7 1 4;
> 2 5 1 4]
>
> This is so since the corresponding rows in B are ' 1 1 4 7' and ' 5 2 4 1'.
>
> take care
> Aiden


When all else fails, one can simply write a loop (OMG!), yes, a loop.

When not loop over A, check if each row matchs any of the rows in B?
this is O(n^3), not good, but for HW or small problem size, not a problem.

For one row check against anothe row check, use something like:

a =
      1 2 3 4
b =
      4 3 1 2
if ( length(intersect(a,b))==length(a)), fprintf('yes\n'); end

yes


(untill a smarter solution is found, that is)

--Nasser

Subject: Identifying Rows with elements in different order

From: Bruno Luong

Date: 17 Oct, 2010 06:37:03

Message: 4 of 5

 intersect(sort(A,2),sort(B,2),'rows')

% Bruno

Subject: Identifying Rows with elements in different order

From: Aidy

Date: 17 Oct, 2010 07:06:03

Message: 5 of 5

thanks Bruno, the sort function is really useful

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