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 that element is multiplied by
If an element of the matrix is a multiple of that element is divided by
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. marks
b Modify the function from part a using NumPys Boolean Array to achieve the same result.
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. marks
d How would you further optimize the function for large matrices eg matrices with millions of
elements What techniques from Python or NumPy could you apply? marks
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
