0

I want to fill the first and the last row and the first and the last column of a grid with 4.

Input:

grid = np.zeros((5, 5))
[[   0.   0.   0.   0.   0. ]
 [   0.   0.   0.   0.   0. ]
 [   0.   0.   0.   0.   0. ]
 [   0.   0.   0.   0.   0. ]
 [   0.   0.   0.   0.   0. ]]

output:

[[   4.   4.   4.   4.   4. ]
 [   4.   0.   0.   0.   4. ]
 [   4.   0.   0.   0.   4. ]
 [   4.   0.   0.   0.   4. ]
 [   4.   4.   4.   4.   4. ]]

I tried to do this with a for loop but is there a better option than that?

0

1 Answer 1

2

To change the values of first and last rows:

grid[[0,-1],:] = 4


To change the values of first and last columns:

grid[:, [0,-1]] = 4
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.