0
$\begingroup$

I have a list of matrices, which I think is better to give an example using the code.

matlist =
{
   {
      {
         {  1.5,     0, 1},
         {  1.5,  0.42, 0}, 
         { 1.73,  0.42, 2}, 
         { 2.06,  0.42, 0},
         {2.305, 0.535, 0}, 
         { 2.55,  0.65, 2},
         { 2.55,  1.09, 0},
         { 2.55,  6.64, 1},
         { 1.59,  6.64, 2},
      }, 
      {
         {-0.11,  -2.4, 1},
         {-0.11, -1.93, 0},
         {-0.02, -1.93, 2}, 
         { 0.41, -1.93, 0},
         { 0.71,-1.765, 0}, 
         { 1.01,  -1.6, 2}, 
         { 1.17,  -1.2, 0},
         { 1.33,  -0.8, 2}, 
         { 1.33, -0.09, 0},
         { 1.33,    3., 1}, 
         { 1.33,    6., 2}, 
         { 1.33,  6.48, 0},
         {1.085,   6.6, 0},
         { 0.84,  6.72, 2}, 
         { 0.51,  6.72, 0}, 
         { 0.38,  6.72, 1},
         { 0.38,  7.14, 0},
      }
   },
   {
      { 3.29,  7.14, 0},
      { 3.29,  6.72, 0},
      { 3.16,  6.72, 2},
      { 2.82,  6.72, 0}, 
      { 2.58,   6.6, 0}, 
      { 2.34,  6.48, 2}, 
      { 2.34,    6., 0},
      { 2.34,    3., 1}, 
      { 2.34, -0.09, 2}, 
      { 2.34, -0.98, 0},
      {2.035, -1.48, 0}, 
      { 1.73, -1.99, 2},
      {1.215, -2.19, 0},
      {  0.7,  -2.4, 2},
      { 0.06, - 2.4, 0}
   }
}

I want to be able to combine the first two matrices into a single matrix (the second in rows added to the first) perhaps using Flatten so that the result looks like this:

{
   {
      {  1.5,     0, 1},
      {  1.5,  0.42, 0}, 
      { 1.73,  0.42, 2}, 
      { 2.06,  0.42, 0},
      {2.305, 0.535, 0}, 
      { 2.55,  0.65, 2},
      { 2.55,  1.09, 0},
      { 2.55,  6.64, 1},
      { 1.59,  6.64, 2},
      {-0.11,  -2.4, 1},
      {-0.11, -1.93, 0},
      {-0.02, -1.93, 2}, 
      { 0.41, -1.93, 0},
      { 0.71,-1.765, 0}, 
      { 1.01,  -1.6, 2}, 
      { 1.17,  -1.2, 0},
      { 1.33,  -0.8, 2}, 
      { 1.33, -0.09, 0},
      { 1.33,    3., 1}, 
      { 1.33,    6., 2}, 
      { 1.33,  6.48, 0},
      {1.085,   6.6, 0},
      { 0.84,  6.72, 2}, 
      { 0.51,  6.72, 0}, 
      { 0.38,  6.72, 1},
      { 0.38,  7.14, 0},
   },
   {
      { 3.29,  7.14, 0},
      { 3.29,  6.72, 0},
      { 3.16,  6.72, 2},
      { 2.82,  6.72, 0}, 
      { 2.58,   6.6, 0}, 
      { 2.34,  6.48, 2}, 
      { 2.34,    6., 0},
      { 2.34,    3., 1}, 
      { 2.34, -0.09, 2}, 
      { 2.34, -0.98, 0},
      {2.035, -1.48, 0}, 
      { 1.73, -1.99, 2},
      {1.215, -2.19, 0},
      {  0.7,  -2.4, 2},
      { 0.06, - 2.4, 0}
   }
}

If possible I would like this to be done for any combined list of matrices and pairs of matrices, so that for example a list of matrices {A,{B,C},{D,E},F,G,{H,I}} could theoretically become {A,BC,DE,F,G,HI} where the double letters are the flattened matrices.

$\endgroup$
3
  • 1
    $\begingroup$ MapAt[Catenate, matlist, {1}], Also note that Mathematica doesn't support tailing comma. $\endgroup$
    – Ben Izd
    Nov 2, 2021 at 14:29
  • $\begingroup$ I would like it more with a general solution, where I might not know the index. If the order in matlist were flipped, I would like what ever function to still produce the same result. $\endgroup$
    – Timevortex
    Nov 2, 2021 at 16:19
  • $\begingroup$ I found this to work okay, but I'm wondering if there is a more elegant solution: Table[If[ArrayDepth[matlist][[ind]]==3,Flatten[matlist[[ind]],1],matlist[[ind]]],{ind,1,Length[matlist]}] $\endgroup$
    – Timevortex
    Nov 2, 2021 at 16:28

0

Your Answer

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

Browse other questions tagged or ask your own question.