# How to ArrayFlatten some matrix which have repeated element

I have many such matrices(whose dimension are all 3*3.) like:

list1={{0,3,0},{4,1,2},{0,5,0}};
list2={{0,6,0},{1,2,8},{0,7,0}};
list3={{0,1,0},{9,5,7},{0,11,0}};
list4={{0,2,0},{5,7,10},{0,12,0}};


Their corners is all $0$.I show they with two dimension format.

As you can see they have some repeated elements(Just can have two repeated elements between two matrices).I want to combine it in this order$\left(\begin{matrix} list1&list2\\list3&list4 \end{matrix} \right)$.in this case,that is: $$\left( \begin{array}{cccc} 0 & 3 & 6 & 0 \\ 4 & 1 & 2 & 8 \\ 9 & 5 & 7 & 10 \\ 0 & 11 & 12 & 0 \\ \end{array} \right)$$ In this simple case,we can know its order should be $\left(\begin{matrix} list1&list2\\list3&list4 \end{matrix} \right)$ after a glance.But in the real case,I just know their neighbouring relations like

{canBeConnect[list1,list2],canBeConnect[list1,list3],
canBeConnect[list3,list4],canBeConnect[list2,list4]}


Any elegant method can do this?

• so you have a set of 3x3 matrices, and you want for example to join two matrices side by side if the right element of the left one is equal to the central element of the right one, and the left element of the right one is equal to the central element of the left one? In general, the "extremal" elements should flag to which other matrices can a matrix be joined, "selecting" all the matrices with the correct central element. Correct? – glS Oct 20 '16 at 11:24
• As @gls point out, for 3x3 matrix it is rather simple. If you have a large matrix, you can convert it into Image[] and use this answer to find the matching edges. – Sumit Oct 20 '16 at 11:50
• @Sumit or this: mathematica.stackexchange.com/q/32612/5478 – Kuba Oct 20 '16 at 12:03
• @glS Yes,they are all 3*3 – yode Oct 20 '16 at 16:24