Q:

Write a NumPy program to create a 2d array with 1 on the border and 0 inside

0

Write a NumPy program to create a 2d array with 1 on the border and 0 inside

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
x = np.ones((5,5))
print("Original array:")
print(x)
print("1 on the border and 0 inside in the array")
x[1:-1,1:-1] = 0
print(x)

Sample Output:

Original array:                                                         
[[ 1.  1.  1.  1.  1.]                                                  
 [ 1.  1.  1.  1.  1.]                                                  
 [ 1.  1.  1.  1.  1.]                                                  
 [ 1.  1.  1.  1.  1.]                                                  
 [ 1.  1.  1.  1.  1.]]                                                 
1 on the border and 0 inside in the array                               
[[ 1.  1.  1.  1.  1.]                                                  
 [ 1.  0.  0.  0.  1.]                                                  
 [ 1.  0.  0.  0.  1.]                                                  
 [ 1.  0.  0.  0.  1.]                                                  
 [ 1.  1.  1.  1.  1.]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now