I have two matrices A
and B
, each with a size of NxM
, where N
is the number of samples and M
is the size of histogram bins. Thus, each row represents a histogram for that particular sample.
What I would like to do is to compute the chi-square
distance between two matrices for a different pair of samples. Therefore, each row in the matrix A
will be compared to all rows in the other matrix B
, resulting a final matrix C
with a size of NxN
and C[i,j]
corresponds to the chi-square
distance between A[i]
and B[j]
histograms.
Here is my python code that does the job:
def chi_square(histA,histB):
esp = 1.e-10
d = sum((histA-histB)**2/(histA+histB+eps))
return 0.5*d
def matrix_cost(A,B):
a,_ = A.shape
b,_ = B.shape
C = zeros((a,b))
for i in xrange(a):
for j in xrange(b):
C[i,j] = chi_square(A[i],B[j])
return C
Currently, for a 100x70
matrix, this entire process takes 0.1 seconds.
Is there any way to improve this performance?
I would appreciate any thoughts or recommendations.
Thank you.