Replace elements with zeros in python assignment expert

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.

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:

Also Read:

  • Team Points in Python | Assignment Expert
  • Ticket selling in Cricket Stadium using Python | Assignment Expert
  • Split the sentence in Python | Assignment Expert
  • String Slicing in JavaScript | Assignment Expert
  • First and Last Digits in Python | Assignment Expert
  • List Indexing in Python | Assignment Expert
  • Date Format in Python | Assignment Expert
  • New Year Countdown in Python | Assignment Expert
  • Add Two Polynomials in Python | Assignment Expert
  • Sum of even numbers in Python | Assignment Expert
  • Evens and Odds in Python | Assignment Expert
  • A Game of Letters in Python | Assignment Expert
  • Sum of non-primes in Python | Assignment Expert
  • Smallest Missing Number in Python | Assignment Expert
  • String Rotation in Python | Assignment Expert
  • Secret Message in Python | Assignment Expert
  • Word Mix in Python | Assignment Expert
  • Single Digit Number in Python | Assignment Expert
  • Shift Numbers in Python | Assignment Expert
  • Weekend in Python | Assignment Expert
  • Shift Numbers in Python | Assignment Expert
  • Temperature Conversion in Python | Assignment Expert
  • Special Characters in Python | Assignment Expert
  • Sum of Prime Numbers in the Input in Python | Assignment Expert
  • Numbers in String-1 in Python | Assignment Expert
  • Replace Elements with Zeros in Python | Assignment Expert
  • Remove Words in Python | Assignment Expert
  • Print Digit 9 in Python | Assignment Expert
  • First Prime Number in Python | Assignment Expert
  • Simple Calculator in Python | Assignment Expert

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

Chủ Đề