7. Write a Python Numpy program to generate a 3x3 matrix with all number 1, then surround the boarder of 3x3 matrix with number 0.
The output is:
3x3 matrix:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Expected Answer:
3x3 matrix:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Surronding with 0:
[[0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]]
Sample Answer:
import numpy as np a = np.ones((3,3)) print("3x3 matrix:") print(a) b = np.pad(a, pad_width=1, mode='constant', constant_values=0) print("\nSurronding with 0:") print(b)
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)