3

Does anyone know how Matlab’s Z=dist(W,P) works? The Matlab documentation does not specify an algorithm that uses the weight matrix, W.

I am using Octave and trying to mimic the behavior. So far, this stack overflow post helped me to prove that Octave’s Z=squareform(pdist(P')) is equivalent to Matlab’s Z=dist(P). I can also designate the elements of Z using a couple of for loops:

function z=dist(w,p)
    for i=1:size(p,2)
        for j=1:size(p,2)
            u=p(:,i);
            v=p(:,j);
            z(i,j)=sum((u-v).^2).^0.5;
        end
    end
end

However, I am unable to find any online documentation on the weight matrix, W. I cannot simply multiply the output of squareform(pdist(P')) by W because the dimensions do not match. I have attempted to premultiply W, e.g. sqrt(W*(P(:,1)-P(:,2)).^2), like this stack overflow post but the output magnitudes are wrong.

Here is an example using Matlab:

P=     
9     7    10
10    1    10
2     3     2
10    6    10

W=      
10    9     5     8
5     2    10    10

>> D = dist(P)
   0      10.0995    1.0000
10.0995      0      10.3441
1.0000    10.3441      0

>> D = dist(W,P)
3.8730     9.0000    3.7417
12.0000    8.3666   12.3693

Thanks in advance for any help you can provide!

Community
  • 1
  • 1
mario314
  • 173
  • 1
  • 4

1 Answers1

0

It appears the Neural Network Toolbox is implemented in Matlab itself, so you can just look at those source files and figure it out.

If you open dist by entering edit dist in the command window, you see that it calls dist.apply or dist.distance to do the actual work, and the latter again dist.apply. Therefore, my guess is that what you are looking for can be found on line 9 of

toolbox/nnet/nnet/nndistance/+dist/apply.m

Type edit dist.apply in the command window to see it.

A. Donda
  • 8,381
  • 2
  • 20
  • 49