numpy一个矩阵如何每隔n行进行合并

问答 北音执念
Lv1 居士
发布在 NumPy   997   1
问答 北音执念   997   1

我现在有个矩阵这样

array([[ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397],
       [ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397],
       [ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397],
       [ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397]])

我想把它变形为这样

array([[[ 0.35441871, 27.2616325 ],
        [ 0.35441871, 27.2616325 ],
        [ 0.35441871, 27.2616325 ],
        [ 0.35441871, 27.2616325 ]],
        
       [[ 3.66211698,  1.31750397],
       [ 3.66211698,  1.31750397],
       [ 3.66211698,  1.31750397],
       [ 3.66211698,  1.31750397]]])

请问我该如何在不使用循环的情况下直接得到,还要考虑更多数据矩阵如

array([[ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397],
       [ 4.36211698,  9.21750397],
       [ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397],
       [ 4.36211698,  9.21750397],
       [ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397],
       [ 4.36211698,  9.21750397],
       [ 0.35441871, 27.2616325 ],
       [ 3.66211698,  1.31750397],
       [ 4.36211698,  9.21750397],])

转为

array([[[ 0.35441871, 27.2616325 ],
        [ 0.35441871, 27.2616325 ],
        [ 0.35441871, 27.2616325 ],
        [ 0.35441871, 27.2616325 ]],
        
       [[ 3.66211698,  1.31750397],
       [ 3.66211698,  1.31750397],
       [ 3.66211698,  1.31750397],
       [ 3.66211698,  1.31750397]],
       
       [[ 4.36211698,  9.21750397],
       [ 4.36211698,  9.21750397],
       [ 4.36211698,  9.21750397],
       [ 4.36211698,  9.21750397]]])

规律就是我有n组数据,需要
n,2n······nn都要和第0行合并
n+1,2n+1······nn+1都要和第1行合并
······
n+n-1,2n+n-1······nn+n-1和第n-1行合并
求教大神该如何解决

版权声明:作者保留权利,不代表意本站立场。如需转载请联系本站以及作者。

参与讨论

回复《 numpy一个矩阵如何每隔n行进行合并

EditorJs 编辑器

沙发,很寂寞~
反馈
to-top--btn