# Matrix of Columns, I want just a regular matrix!

I've been at this problem for a couple of hours now and no success. I'm very new to Mathematica I hope someone can help. I have the following

 matrix =
{{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}},
{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {1/Sqrt[6]}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}},
{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {1/Sqrt[3]}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}},
{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {1/Sqrt[2]}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}}};


mut what I want is just {{0,0,0,0,0,0}, {0,0,0,0,0,0}, ... and so on for 48 rows. I'm pretty sure I need to do some funky Flatten, but how?

• You should try with ArrayFlatten maybe. See: reference.wolfram.com/language/ref/ArrayFlatten.html Commented Jan 6, 2016 at 16:05
• Array flatten also does nothing..... Commented Jan 6, 2016 at 16:14
• Partition[Flatten[matrix], 6]? Commented Jan 6, 2016 at 16:40
• Stelios you are an absolute legend, and anyone that disagrees, we'll get them to pm me and I'll persuade them otherwise. Thank you! Commented Jan 6, 2016 at 16:48
• @user35529 LOL, thanks! Commented Jan 6, 2016 at 17:02

Upon further reflection,

Flatten[Flatten[yourmatrix, {3, 4}], 1]


I missed the extra higher level of structure. The inner Flatten gets the low level, the outer the high level.

• Didn't seem to do anything..... Commented Jan 6, 2016 at 16:38
• Worked for me in 10.1 on Mac Commented Jan 6, 2016 at 16:42
• Actually I got a 8 by 6 matrix, all zeroes.... Commented Jan 6, 2016 at 16:42
• I appreciate the effort, stelios's suggestion above worked. Thanks. :) Commented Jan 6, 2016 at 17:06
• @user35529 it can also be done in a single Flatten call: Flatten[matrix, {{2, 1}, {3, 4}}]. Actually, with more non-zero elements in the matrix, you will see that this yields the correct result, whereas the code of this answer does not. Commented Jan 7, 2016 at 16:57

 data =
{{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}},
{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {1/Sqrt[6]}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}},
{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {1/Sqrt[3]}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}},
{{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {1/Sqrt[2]}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}},
{{0}, {0}, {0}, {0}, {0}, {0}}, {{0}, {0}, {0}, {0}, {0}, {0}}}};

Flatten[Map[Flatten, data, {2, -2}], 1]

{{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 1/Sqrt[6], 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 1/Sqrt[3], 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 1/Sqrt[  2], 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}}

• This is equivalent to Flatten[matrix, {{1, 2}, {3, 4}}]. But as I mentioned on the accepted answer, there are many ways to flatten the four levels together and it is hard to tell what result OP actually expects, especially when the majority of the elements are zeros. Commented Jan 7, 2016 at 17:08

as I understand you have at the top level a 6x8 matrix which really represents 48 rows, flatten the top level first then flatten each row:

   Flatten /@ Flatten[matrix, 1]


or ..

   First@*Transpose /@ Flatten[matrix, 1]


This is essentially the reverse order of operations as JohnD's method, a matter of what you find more convenient.

Yet another

Join @@ Map[Flatten, matrix, {2}]