8
$\begingroup$

Really sorry if this question is a duplicate. I couldn't find exactly what I was looking for.

Here is a list I have:

{{{0. + 0.1875 I, 0. + 0.105469 I, 0. + 0.03125 I, 0. + 0.00390625 I, 
   0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I}, {0, 0, 0, 0, 0, 0, 0, 
   0}, {0, 0, 0, 0, 0, 0, 0, 0}}, {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0,
    0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}, {{0, 0, 0, 0, 0, 0, 0, 
   0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}}

Here it is again in MatrixForm:

I would like to join all triplets of sub arrays in each colums so as to have only three long columns an end up with a 2-dimensional array.

Can't seem to find a way using Flatten. Any help would be awesome!

$\endgroup$
  • $\begingroup$ To put it more plainly, I wish to flatten every column independently. $\endgroup$ – jrekier Oct 31 '16 at 9:55
  • $\begingroup$ @Feyre Nope, this just gives a 2d array with all the small columns next to one another :( $\endgroup$ – jrekier Oct 31 '16 at 10:00
  • $\begingroup$ Transpose@(Join @@@ Table[list[[i]], {i, 3}]) $\endgroup$ – Feyre Oct 31 '16 at 10:10
  • $\begingroup$ One way is Flatten /@ data // Transpose, there could potentially be even simpler ways. $\endgroup$ – C. E. Oct 31 '16 at 10:26
  • $\begingroup$ Many thanks to both of you :) $\endgroup$ – jrekier Oct 31 '16 at 10:32
7
$\begingroup$

Flatten can be used to reshape dimensions by making use of its second parameter.

Flatten[#, {2, 3}] &@Transpose@data// MatrixForm

Transpose places your columns in rows then Flatten flattens over dimension 2 (each cell in each row - these contain a sub-list) and then over dimension 3 (each value in each sub-list of each row). This gives the the three columns you are seeking.

Hope this helps.

| improve this answer | |
$\endgroup$
  • 4
    $\begingroup$ +1 That can simplified to Flatten[data, {1, 3}] // MatrixForm. $\endgroup$ – WReach Oct 31 '16 at 17:54
  • $\begingroup$ @WReach Great improvement. Thanks. $\endgroup$ – Edmund Oct 31 '16 at 20:43
5
$\begingroup$

If it isn't closed, it should get an answer. As far as I can tell the shortest way is

Join @@@ list // Transpose

or as @C.E. pointed out

Flatten /@ list // Transpose
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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