I have a 1D array that I need to be expanded to 3D, with the original array copied across axis=0
.
Currently, I have a setup like this:
import numpy as np
x = np.array((1, 2, 3, 4, 5))
y = np.zeros((len(x), 5, 5))
for i in range(5):
for j in range(5):
y[:, i, j] = x
print(y)
[[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]]
[[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]]
[[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4.]]
[[5. 5. 5. 5. 5.]
[5. 5. 5. 5. 5.]
[5. 5. 5. 5. 5.]
[5. 5. 5. 5. 5.]
[5. 5. 5. 5. 5.]]]
It strikes me that there should be an easier way to do this than with nested for
loops, but anything that shows up with a cursory search shows how to cut up a long 1D array and make it 3D, but not copying the initial dimension into 2 more dimensions.