5
$\begingroup$

I have this list

{{{0.05792, 0.31744}, 0., 0., 0., 0., 
  0.}, {0., {0.28832, 0.49024}, {0.17173, 0.386393}, 0., 0., 
  0.}, {0., {0.17173, 0.386393}, {0.104, 0.352}, 0., 0., 0.}, {0., 0.,
   0., {0.30752, 0.38464}, {0.322232, 0.260264}, {0.214663, 
   0.107331}}, {0., 0., 
  0., {0.322232, 0.260264}, {0.392, 0.496}, {0.277128, 
   0.415692}}, {0., 0., 
  0., {0.214663, 0.107331}, {0.277128, 0.415692}, {0.2, 0.4}}}

or in the matrix form: enter image description here

I would like to convert this list to a list of two matrices, each of the same size as the input matrix, i.e. 6 by 6, where the non-zero entries for the first matrix are given by the first element of the lists in the input matrix and the same for the second matrix.

If it helps I can also create a matrix where the zeros are replaced by a list of two zeros to match the dimension.

$\endgroup$

2 Answers 2

5
$\begingroup$
lst1=#1&@@@#&/@lst

{{0.05792, 0., 0., 0., 0., 0.}, {0., 0.28832, 0.17173, 0., 0., 0.},

{0., 0.17173, 0.104, 0., 0., 0.}, {0., 0., 0., 0.30752, 0.322232, 0.214663},

{0., 0., 0., 0.322232, 0.392, 0.277128}, {0., 0., 0., 0.214663, 0.277128, 0.2}}

lst2=#2&@@@#&/@lst

{{0.31744, 0., 0., 0., 0., 0.}, {0., 0.49024, 0.386393, 0., 0., 0.},

{0., 0.386393, 0.352, 0., 0., 0.}, {0., 0., 0., 0.38464, 0.260264, 0.107331},

{0., 0., 0., 0.260264, 0.496, 0.415692}, {0., 0., 0., 0.107331, 0.415692, 0.4}}

$\endgroup$
9
$\begingroup$
f = Thread @* Map[Thread];

With listsas your input list:

MatrixForm /@ f[lists]

enter image description here

Alternatively, you can replace 0. with {0., 0.} in lists

paddedlists = lists /. 0. -> {0., 0.};

and use paddedlists with Transpose or with Flatten to get the same result:

f @ lists == Flatten[paddedlists, {{3}, {2}, {1}}] == Transpose[paddedlists, {3, 2, 1}]
True
$\endgroup$
1
  • $\begingroup$ Thank you! Exactly what I wanted! $\endgroup$
    – tomeq
    Commented May 31, 2021 at 6:26

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.