Reputation: 362
i want a good method to insert the one inside zeros in a cross pattern with ones on top rows and bottom rows.
import numpy as np
a = np.zeros((n,n), dtype=int)
a[0,:] = 1
a[-1,:] = 1
for i in range(1,n):
a[i,-i-1] = 1
print(a)
Output:
[[1 1 1]
[0 1 0]
[1 1 1]]
Upvotes: 0
Views: 36
Reputation: 150785
You can use np.eye
and reverse the rows, then assign with slices:
a = np.eye(n, dtype=int)[::-1]
a[[0,-1]] = 1
print(a)
Output:
[[1 1 1 1]
[0 0 1 0]
[0 1 0 0]
[1 1 1 1]]
Upvotes: 1