Let's say that I have an array x = np.random.normal(size=(300, 300)) and that I wish to create a diagonal matrix of size (2, 2, 300, 300) using x. My first approach was to just do
import numpy as np
x = np.random.normal(size=(300, 300))
array = np.array([
[x, 0.],
[0., x]
])
However, when doing so I get an array of size (2, 2). Is there a numpy function for recasting the elements to the same size? Given that all the arrays of the list are the same shape, and that all other elements are floats.
Edit
I might add that just defining an empty array and setting the diagonal to x is not a solution.
Edit 2 Here's a sample
import numpy as np
x = np.random.normal(size=(3, 3))
array = np.zeros((2, 2, *x.shape))
array[0, 0] = array[1, 1] = x
print(array)
Yielding
[[[[-1.57346701 -1.00813871 -0.72318135]
[ 0.11852539 1.144298 1.38860739]
[ 0.64571669 0.47474236 0.294049 ]]
[[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]]]
[[[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]]
[[-1.57346701 -1.00813871 -0.72318135]
[ 0.11852539 1.144298 1.38860739]
[ 0.64571669 0.47474236 0.294049 ]]]]
np.random.normal(size=(3, 3))and show us the expected output?