Reputation: 11
So I've got a numpy array like this:
a = np.array([[1, 2, 3],
[2, 3, 4],
[4, 5, 6]])
and I want to convert it into an array like this:
[[[1, 1, 1], [2, 2, 2], [3, 3, 3]],
[[2, 2, 2], [3, 3, 3], [4, 4, 4]],
[[4, 4, 4], [5, 5, 5], [6, 6, 6]]]
How would you do it?
Upvotes: 1
Views: 48
Reputation: 262484
Use numpy.repeat
on the array with one extra dimension:
np.repeat(a[...,None], 3, axis=2)
Or numpy.tile
:
np.tile(a[...,None], (1,1,3))
Output:
array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[2, 2, 2],
[3, 3, 3],
[4, 4, 4]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]])
Upvotes: 3