阵列要重塑

时间:2018-12-14 11:49:48

标签: python

我有一个很大的数组,像这样:

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      ]])]

我想将其更改为一个大矩阵,例如:

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 ]]

我该怎么办?你能帮我么?谢谢你提前!!!

2 个答案:

答案 0 :(得分:2)

使用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       ]])

答案 1 :(得分:0)

我认为array关键字是错误的。您只需以列表格式声明2个数组,然后将其串联即可。

A1 = [[ 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     ]]
A2 = [[ 2.      ,  0.        ,  0.       ],
            [-2.      ,  0.        ,  0.       ],
            [-1.      ,  1.73205081,  0.       ],
            [ 1.      , -1.73205081,  0.       ],
            [ 0.      ,  0.        ,  3.1      ],
            [ 0.      ,  0.        , -3.1      ]]

然后

B = A1 + A2

B将是您想要的矩阵。