首页 > 解决方案 > Array to be reshaped

问题描述

I have a large array like this:

A =
   [ array([[ 0.      ,  0.        ,  0.       ],
           [ 1.       ,  0.        ,  0.       ],
           [-1.       ,  0.        ,  0.       ],
           [-0.5      ,  0.8660254 ,  0.       ],
           [ 0.5      , -0.8660254 ,  0.       ],
           [ 0.       ,  0.        ,  1.55     ],
           [ 0.       ,  0.        , -1.55     ]])
     array([[ 2.      ,  0.        ,  0.       ],
            [-2.      ,  0.        ,  0.       ],
            [-1.      ,  1.73205081,  0.       ],
            [ 1.      , -1.73205081,  0.       ],
            [ 0.      ,  0.        ,  3.1      ],
            [ 0.      ,  0.        , -3.1      ]])]

I would like to change it into one big matrix such as:

B = [ [0.    0.         0.   ]
      [1.    0.         0.   ]
      [-1.   0.         0.   ]
      [-0.5  0.8660254  0.   ]
      [0.5  -0.8660254  0.   ]
      [0.    0.         1.55 ]
      [0.    0.        -1.55 ]
      [2.    0.          0.  ]
      [-2.   0.          0.  ]
      [-1.   1.73205081  0.  ]
      [1.   -1.73205081  0.  ]
      [0.    0.          3.1 ]
      [0.    0.         -3.1 ]]

How can I do it? Can you please help me? Thanks for advance !!!

标签: python

解决方案


使用np.concatenate.

定义A

A =  [np.array([[ 0.      ,  0.        ,  0.       ],
       [ 1.       ,  0.        ,  0.       ],
       [-1.       ,  0.        ,  0.       ],
       [-0.5      ,  0.8660254 ,  0.       ],
       [ 0.5      , -0.8660254 ,  0.       ],
       [ 0.       ,  0.        ,  1.55     ],
       [ 0.       ,  0.        , -1.55     ]]),
     np.array([[ 2.      ,  0.        ,  0.       ],
        [-2.      ,  0.        ,  0.       ],
        [-1.      ,  1.73205081,  0.       ],
        [ 1.      , -1.73205081,  0.       ],
        [ 0.      ,  0.        ,  3.1      ],
        [ 0.      ,  0.        , -3.1      ]])]

只需使用np.concatenate(A),它将加入数组序列:

np.concatenate(A)

array([[ 0.        ,  0.        ,  0.        ],
       [ 1.        ,  0.        ,  0.        ],
       [-1.        ,  0.        ,  0.        ],
       [-0.5       ,  0.8660254 ,  0.        ],
       [ 0.5       , -0.8660254 ,  0.        ],
       [ 0.        ,  0.        ,  1.55      ],
       [ 0.        ,  0.        , -1.55      ],
       [ 2.        ,  0.        ,  0.        ],
       [-2.        ,  0.        ,  0.        ],
       [-1.        ,  1.73205081,  0.        ],
       [ 1.        , -1.73205081,  0.        ],
       [ 0.        ,  0.        ,  3.1       ],
       [ 0.        ,  0.        , -3.1       ]])

推荐阅读