I want to create a function to iterate through a list and a matrix, and return values based on the formula:
Matrix cell value - list value
The code looks like this:
def calculate(matrix, list):
out_matrix = np.empty_like(matrix)
for k in list:
for i in matrix.shape[0]:
for j in matrix.shape[1]:
out_matrix[i,j] = matrix[i,j] - k
return out_matrix
This code iterates all matrix for each value in list. But I need to increment my values to calculate only once. What I want is to iterate the matrix once, for each position in the list.
For example:
list = [1, 2, 3, 4, 5, 6]
matrix = np.array([(1,2,3),
(4,5,6)])
The returned values should be:
matrix[0,0] - list [0] = 1 -1 = 0
matrix[0,1] - list [1] = 2 -2 = 0
.....etc
And the out_matrix will store those values in the same place as the input matrix. Thanks for the help! :)