Replace Elements with Zeros
Given a MxN matrix, write a program to replace all elements that do not belong to principal-diagonal & anti-diagonal with Zeros.
Principal-diagonal elements are the set of elements of a matrix that lie on the line joining the top left corner to the bottom right corner.
Anti-diagonal elements are the set of elements of a matrix that lie on the line joining the bottom left corner to the top right corner.Input
The first line of input will contain two space-separated integers, denoting MxN matrix.
The next M lines will contain N space-separated integers.Output
The output should be MxN matrix by replacing elements that do not belong to principal-diagonal and anti-diagonal with Zeros. Print each row as a space separated integers in a single line.Explanation
For example if the M and N are 5 and 5. Read the elements of 5 rows in next 5 lines. If the given elements are
def print_1[mat, n, m]:
for i in range[n]:
for j in range[m]:
print[mat[i][j], end=" "]
print[]
def makediagonalnotzero[mat, n, m]:
for i in range[n]:
for j in range[m]:
# right and left diagonal condition
if [i == j or [i + j + 1] == n]:
[mat[i][j]]
else:
mat[i][j] = 0
# print resultant matrix
print_1[mat, n, m]
# Driver code
if __name__ == "__main__":
n = 3
m = 3
mat = [[2, 1, 7],
[3, 7, 2],
[5, 4, 9]]
makediagonalnotzero[mat, n, m]
Harry August 23, 2022 Today, we will see how to replace elements with zeros in Python. We will write two programs for that, in the first one, we will see how to replace elements with zeros in a matrix created using lists in Python, and in the second one, we will
use NumPy. Also Read:1. Replace Elements with Zeros in a matrix of List in Python
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
for i in range[len[matrix]]:
for j in range[len[matrix]]:
matrix[i][j] = 0
print[matrix]
Output:
2. Replace Elements with Zeros in a matrix of List in Python
import numpy as np
matrix = np.array[[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]]
for i in range[len[matrix]]:
for j in range[len[matrix]]:
matrix[i][j] = 0
print[matrix]
Output:
Author: Harry
Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at Thank you
Post navigation
Categories
Our Services
Contact us on WhatsApp at +91-9760648231 for help with your-
python project
python
assignment
python management assignment
python management project
python freelancer
assignment helper
machine learning expert
machine learning helper
ai ml helper
ai ml expert
ml freelancer
machine learning homework helper
final year project
project helper
assignment expert
project expert
programming expert
homework helper
homework expert