How to find mutual arrays of multiple matrices with different length?

2 views (last 30 days)
Hello!
I have 4 different 2D matrices which are equal in number of columns and different in number of rows. In these matrices there are some rows which have the same value for columns 2 : 4. I mean A(i,2:4)=B(k,2:4)=C(l,2:4)=D(m,2:4). I need to find them and put them in a 3D matrix like E. In this 3D matrix E(:,:,1) are all the mutual rows found in matrix A, for example A(i,: ). Matrix E(:,:,2) are all the mutual rows found in matrix B, for example B(k,: ). Matrix E(:,:,3) are all the mutual rows found in matrix C, for example C(l,: ). Matrix E (:,:, 4) are all the mutual rows found in matrix D, for example D(m,: ). And E(1,2:4,1)= E(1,2:4,2)= E(1,2:4,3)= E(1,2:4,4) and the same for all rows of matrix E.
I started with identifying the matrix with minimum number of rows and finding the matches of column 2:4 in each row in other matrices with an if condition; and if matched putting them in the 3D matrix. It is practical as long as there are just two matrices to compare. Now I have trouble handling these four matrices.
Is there anyone who can help me with this?

Accepted Answer

the cyclist
the cyclist on 6 Nov 2014
I think you could do this with the intersect command.
[AB,ia,ib] = intersect(A(:,2:4),B(:,2:4),'rows')
will give you the common rows of those columns of A and B, with indices to those rows.
Seems like you could either do this with every combination of matrices, or work your way forward then back. A bit tedious, but it's straightforward.
  1 Comment
Negar
Negar on 6 Nov 2014
Thank you very much .... This works perfectly as long as there is just two matrices. It doesn't give me intersect of more than two matrices.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!