+ 1
why we used mat and not filter? why we didnt used filter[n/2,n/2] instead mat[n/2,n/2]?
import numpy as np def filter_matrix(center,n): filter = -1*np.ones((n,n)) mat[n/2,n/2] = center return mat.tolist() ...by example if i run this code ; filter_matrix (8,3) [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]
5 ответов
+ 3
I don't know what mat is (and if you to run the program, you'll probably see that python doesn't know that neither) , but if you use it with square brackets, I assume it's the name of a variable/list. It wasn't defined, so you can't use it. filter is not an appropriate name for a variable because there is a built-in function with the same name
+ 2
I wasn't able to run your code. I had to change it to
import numpy as np
def filter_matrix(center,n):
filter = -1*np.ones((n,n))
filter[n//2, n//2] = center
return filter.tolist()
Replacing all occurrences of filter by mat should also work. So could you please explain the question?
Also, I think we need // instead of /, so the indices do not become floats.
+ 2
Kishalaya Saha Anna Hi! ,I don't know what is mat, it's not even defined ,thats why I asked why mat[n/2,n/2] , (was solving a question and found this solution )
+ 2
Anna ,Kishalaya Saha and thanks ☺
+ 2
Maybe sama baluom is looking for the 'map' Function. map (function, list) will apply a Function to all Items of a List.