Well, suppose that I have an image with dimensions n by n. which is ordered lexicographically on a column vector with dimensions n^2 by 1.
I want to calculate following affinity matrix for image:
Clearly, the matrix is symmetric and we can reduce the calculations using this property, this is my attempt to implement the above matrix calculation:
def w(R,sigma,x):
n=x.shape[0]
w=np.zeros_like(x).astype(np.float64)
img=x.ravel()
for i in range(img.shape[0]):
for j in range(img.shape[0]):
if(max(abs(i//n-j//n),abs(i%n-j%n))>R or i==j):
w[i//n,j%n]=0
continue
w[i//n,j%n] = np.exp((-1*(int(img[i])-int(img[j]))**2)/sigma)
return w
where R is the threshold for norm, sigma is the exponential damper and x is the image.
However, this code is very ineffective, and its order is n^2, do you have any better idea for implementing the code faster?
