简体   繁体   中英

Creating a matrix of matrices using numpy.array()

I've been trying to create a matrix of matrices using the numpy function numpy.array() and am facing difficulties

I'm specifically trying to create the following matrix

[

[

 [                    [
   [ 1 ,2 ]             [ 1 , 2 ]
   [ 3 ,4 ]             [ 3 , 4 ]
 ]               ,    ]

]

[

 [                    [
   [ 1 ,2 ]             [ 1 , 2 ]
   [ 3 ,4 ]             [ 3 , 4 ]
 ]               ,    ]  

]

]

more precisely like this one

I've tried the following line in Jupyter

x = np.array( [
        [   [ 1,2 ] ,[ 3, 4]  ] ,  [   [ 1,2 ] ,[ 3, 4]  ] ,
        [   [ 1,2 ] ,[ 3, 4]  ] ,  [   [ 1,2 ] ,[ 3, 4]  ]
])

but what it does is puts all the 2X2 matrices in row-wise form.

I'm not able to take 2 ( 2X2 ) matrices in row form and replicate them in columns or 2 ( 2X2 ) matrices in column form and replicate them into rows

Any idea how to create this using numpy.array() or any other approach( using numpy functions )

it seem simple but I'm finding difficulties in formulating the code. Thanks in advance.

>>> a = np.array([[[[1,2],[3,4]], [[1,2], [3,4]]], [[[1,2],[3,4]], [[1,2], [3,4]]]])
>>> a
array([[[[1, 2],
         [3, 4]],

        [[1, 2],
         [3, 4]]],


       [[[1, 2],
         [3, 4]],

        [[1, 2],
         [3, 4]]]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM