I would like to create an 3D array in numpy as follow :
[ 0 1 0 1 0 1
0 1 0 1 0 1
0 1 0 1 0 1
0 1 0 1 0 1
0 1 0 1 0 1 ] ...
Is there a nice way to write it ?
Using np.tile:
import numpy as np
a = np.array([0, 1])
my_tiled_array = np.tile(a, (3, 3))
Result:
array([[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1]])
Edit:
As @DSM suggests in a comment, if you really want a 3D array -- which is not entirely clear to me from your code sample -- you can use:
my_3d_tiled_arr = np.tile(a, (3, 3, 3))
Result:
array([[[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1]],
[[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1]],
[[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 0, 1]]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With