4
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ You should try with ArrayFlatten maybe. See: reference.wolfram.com/language/ref/ArrayFlatten.html $\endgroup$
    – mattiav27
    Commented Jan 6, 2016 at 16:05
  • $\begingroup$ Array flatten also does nothing..... $\endgroup$
    – user35529
    Commented Jan 6, 2016 at 16:14
  • 2
    $\begingroup$ Partition[Flatten[matrix], 6]? $\endgroup$
    – Stelios
    Commented Jan 6, 2016 at 16:40
  • $\begingroup$ 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! $\endgroup$
    – user35529
    Commented Jan 6, 2016 at 16:48
  • 1
    $\begingroup$ @user35529 LOL, thanks! $\endgroup$
    – Stelios
    Commented Jan 6, 2016 at 17:02

4 Answers 4

1
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ Didn't seem to do anything..... $\endgroup$
    – user35529
    Commented Jan 6, 2016 at 16:38
  • $\begingroup$ Worked for me in 10.1 on Mac $\endgroup$
    – John Doty
    Commented Jan 6, 2016 at 16:42
  • $\begingroup$ Actually I got a 8 by 6 matrix, all zeroes.... $\endgroup$
    – user35529
    Commented Jan 6, 2016 at 16:42
  • $\begingroup$ I appreciate the effort, stelios's suggestion above worked. Thanks. :) $\endgroup$
    – user35529
    Commented Jan 6, 2016 at 17:06
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Jan 7, 2016 at 16:57
4
$\begingroup$

How about

 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}}
$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Jan 7, 2016 at 17:08
3
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

Yet another

Join @@ Map[Flatten, matrix, {2}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.