Write a function that computes the square value of all integers of a matrix.Prototype: def square_matrix_simple(matrix=[]):matrix is a 2 dimensional arrayReturns a new matrix:Same size as matrixEach value should be the square of the value of the inputInitial matrix should not be modifiedYou are not allowed to import any moduleYou are allowed to use regular loops, map, etc.
Question
Write a function that computes the square value of all integers of a matrix.Prototype: def square_matrix_simple(matrix=[]):matrix is a 2 dimensional arrayReturns a new matrix:Same size as matrixEach value should be the square of the value of the inputInitial matrix should not be modifiedYou are not allowed to import any moduleYou are allowed to use regular loops, map, etc.
Solution
Sure, here is a step-by-step solution to the problem:
- Define the function
square_matrix_simplethat takes a matrix as input. - Create an empty list called
result_matrixto store the squared values. - Iterate over each row in the matrix using a for loop.
- Inside the loop, create an empty list called
row_resultto store the squared values of the current row. - Iterate over each element in the current row using another for loop.
- Square the current element by multiplying it with itself and append the squared value to
row_result. - After iterating over all elements in the row, append
row_resulttoresult_matrix. - After iterating over all rows in the matrix, return
result_matrix.
Here is the code implementation:
def square_matrix_simple(matrix):
result_matrix = []
for row in matrix:
row_result = []
for element in row:
squared_value = element ** 2
row_result.append(squared_value)
result_matrix.append(row_result)
return result_matrix
This function takes a matrix as input and returns a new matrix with the same size, where each value is the square of the corresponding value in the input matrix. The initial matrix is not modified, and no external modules are used.
Similar Questions
You are given a matrix of integers. Your task is to write a program that takes the dimensions of the matrix and its elements as input, and then prints the matrix.Input:3 31 2 34 5 67 8 9Output:1 2 34 5 67 8 9
To implement a magic square, we use the concept of matrices. Select all the statements that are True in this context. One possible Implementation of matrices is through nested lists in Python. NumPy is one of the standard libraries associated with implementing matrices. Implementation of matrices is only through lists in Python. None of the above
Write a program to check if the given matrix is a magic square or not
Write a python program that defines a matrix and prints
Accept two square matrices A and B of dimensions n×n as input and compute their product AB.The first line of the input will contain the integer n. This is followed by 2n lines. Out of these, each of the first n lines is a sequence of comma-separated integers that denotes one row of the matrix A. Each of the last n lines is a sequence of comma-separated integers that denotes one row of the matrix B.Your output should again be a sequence of n lines, where each line is a sequence of comma-separated integers that denotes a row of the matrix AB.CODE WITH ALGORITHM# CODE WITH ALGORITHM# 1. Accept an integer 'n' as input.# 2. Initialize empty lists 'A' and 'B' to store matrices.# 3. Loop 'n' times to accept matrix A:# 3.1. Initialize an empty list 'row'.# 3.2. Split the input line by ',' and convert each element to an integer, adding it to 'row'.# 3.3. Append 'row' to matrix 'A'.# 4. Loop 'n' times to accept matrix B:# 4.1. Initialize an empty list 'row'.# 4.2. Split the input line by ',' and convert each element to an integer, adding it to 'row'.# 4.3. Append 'row' to matrix 'B'.# 5. initialize matrix 'C' as a zero-matrix:# 5.1. Loop 'n' times to initialize each element of 'C' to 0.# 6. [Perform matrix product:]# 6.1. Loop 'n' times to iterate through rows of 'A':# 6.1.1. Loop 'n' times to iterate through columns of 'B':# 6.1.1.1. Initialize a variable 'esum' to 0.# 6.1.1.2. Loop 'n' times to iterate through the elements of 'A' and 'B':# 6.1.1.2.1. Add the product of corresponding elements from 'A' and 'B' to 'esum'.# 6.1.1.3. Update the corresponding element in 'C' with 'esum'.# 6.1.1.4. If it's not the last element in the row:# 6.1.1.4.1. Print the element followed by a comma.# 6.1.1.4.2. Else, print the last element in the row without a comma.Sample Test CasesDownload All Test Case 1InputExpected OutputActual Output21,23,45,67,819,2243,50Test Case 2InputExpected OutputActual Output31,0,00,1,00,0,12,3,45,9,110,12,132,3,45,9,110,12,13
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.