Is my modification on Jeffrey's divergence code correct?
2 views (last 30 days)
Show older comments
I use Matlab code available on file exchange to compare the degree of similarity between 2 images I got an unexpected result which has too many NaN.
Therefore, I debug the code to find the issue and try to solve it.
original code
function d=jeffrey_divergence(XI,XJ)
m=size(XJ,1); % number of samples of p
p=size(XI,2); % dimension of samples
assert(p == size(XJ,2)); % equal dimensions
assert(size(XI,1) == 1); % pdist requires XI to be a single sample
d=zeros(m,1); % initialize output array
for i=1:m
for j=1:p
m=(XJ(i,j) + XI(1,j)) / 2;
if m ~= 0 % if m == 0, then xi == xj == 0
d(i,1) = d(i,1) + (XI(1,j) * log(XI(1,j) / m)) + (XJ(i,j) * log(XJ(i,j) / m));
end
end
end
Modified code
function d=jeffrey_divergence(XI,XJ)
m=size(XJ,1); % number of samples of p
p=size(XI,2); % dimension of samples
assert(p == size(XJ,2)); % equal dimensions
assert(size(XI,1) == 1); % pdist requires XI to be a single sample
d=zeros(m,1); % initialize output array
for i=1:m
for j=1:p
m=(XJ(i,j) + XI(1,j)) / 2;
if m ~= 0 % if m == 0, then xi == xj == 0
if XJ(i,j)~=0 & XI(1,j)~=0 % I was added this line
d(i,1) = d(i,1) + (XI(1,j) * log(XI(1,j) / m)) + (XJ(i,j) * log(XJ(i,j) / m));
end
end
end
end
After I add if XJ(i,j)~=0 & XI(1,j)~=0 I get result without any NaN. But I still worry about the result accuracy.
Now I have two questions:
1. Is Jeffrey's divergence code correct? I don't know how to evaluate it!
2. This line of code 'if XJ(i,j)~=0 & XI(1,j)~=0', I added could affect the result accuracy?
0 Comments
Answers (0)
See Also
Categories
Find more on Get Started with Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!