0

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.

2
  • np.array replicates the nesting of the input brackets exactly. Commented Mar 29, 2020 at 10:48
  • numpy does not dIsplay a (2,2,2,2) array like that. Don't confuse display layout with actual data shape Commented Mar 29, 2020 at 11:41

1 Answer 1

1
>>> 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]]]])
Sign up to request clarification or add additional context in comments.

5 Comments

yes, but i want them replicated into columns as well , a 2X2 matrix which has 2X2 matrices as elements
So you mean a 4D matrix ? I edited the answer, let me know if it's what you're looking for
yes, a 4D matrix , a 2X2 matrix the elements of which are 2X2 matrices
The shape is (2,2,2,2).
right that's what i was looking for , took me a while to realise

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.