Issue
I want to efficiently print a matrix in python that follows a specific pattern in the columns of 5 0s then 3 1s then 5 0s and so on and so forth as shown below for 1000 rows:
0000
0000
0000
0000
0000
1111
1111
1111
0000
0000
0000
0000
0000
1111
1111
1111
...
Solution
You can use a combination of np.block
and list comprehension:
out = np.block([[np.zeros((5, 4))],[np.ones((3, 4))]] * 125).astype(int)
This can be functionalized to answer similar questions like so:
def block_repeat(size, *args):
block = np.block([[a] for a in args])
return np.resize(block, (size,) + block.shape[1:])
block_repeat(1000, np.zeros((5,4)), np.ones((3,4)))
Out[]:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
...,
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
Answered By - Daniel F
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.