"stacking" Arrays In A New Dimension?
Consider, for reference: >>> x, y = np.ones((2, 2, 2)), np.zeros((2, 2, 2)) >>> np.concatenate((x, y, x, y), axis=2) array([[[ 1., 1., 0., 0., 1., 1., 0.,
Solution 1:
You can do it as follows:
>>>xx = x[..., None, :]>>>yy = y[..., None, :]>>>np.concatenate((xx, yy, xx, yy), axis=2).shape
(2, 2, 4, 2)
>>>np.concatenate((xx, yy, xx, yy), axis=2)
array([[[[ 1., 1.],
[ 0., 0.],
[ 1., 1.],
[ 0., 0.]],
[[ 1., 1.],
[ 0., 0.],
[ 1., 1.],
[ 0., 0.]]],
[[[ 1., 1.],
[ 0., 0.],
[ 1., 1.],
[ 0., 0.]],
[[ 1., 1.],
[ 0., 0.],
[ 1., 1.],
[ 0., 0.]]]])
>>>
What this example does is change the shape (no data is copied) of the arrays. Slicing with None
or equivalently np.newaxis
adds an axis:
>>> xx.shape
(2, 2, 1, 2)
>>> xx
array([[[[ 1., 1.]],
[[ 1., 1.]]],
[[[ 1., 1.]],
[[ 1., 1.]]]])
>>>
Post a Comment for ""stacking" Arrays In A New Dimension?"