Question: In this question, you will write a Python function to modify the elements of a square matrix. The input to the function should a matrix.

In this question, you will write a Python function to modify the elements of a square matrix.
The input to the function should a matrix. The code should check whether the input matrix is square
or not. If it is not square, the Python function should print "the matrix is not square". If the
matrix is square, the Python function should modify the elements of the matrix as follows.
If an element of the matrix is odd and a multiple of 5, that element is multiplied by 5.
If an element of the matrix is a multiple of 4, that element is divided by 5.
Other elements of the matrix are not modified.
Print the modified matrix.
Use for loops to modify the elements of the matrix.
a) Write the function using for loops. (5 marks)
b) Modify the function from part (a) using NumPys Boolean Array to achieve the same result.
(3 marks).
c) What are the pros and cons of using for loops versus NumPys Boolean Array for this task?
Explain in terms of efficiency, readability, and maintainability. (3 marks)
d) How would you further optimize the function for large matrices (e.g., matrices with millions of
elements)? What techniques from Python or NumPy could you apply? (2 marks)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!