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.
m, n = input('m n ').split()
m, n = int(m), int(n)
matr = []
for i in range(m):
matr.append(list(map(int, input().split())))
for i in range(m):
for j in range(n):
if not((i == j) or (i == n - j - 1)):
matr[i][j] = 0
for el in matr:
print(*el)
Comments
Leave a comment