Home > sgwt_toolbox > demo > distanz.m

distanz

PURPOSE ^

DISTANZ : calculates the distances between all vectors in x and y.

SYNOPSIS ^

function d = distanz(x,y,type)

DESCRIPTION ^

 DISTANZ : calculates the distances between all vectors in x and y.

 usage:
    d = distanz(x,y);

 inputs:
    x      matrix with col vectors
    y      matrix with col vectors (default == x)
    type   the type of algorithm that is used (default==3)

 outputs:
    d      distance matrix, not squared

 note:
    part of the code is inspired by dist.m of the nntoolbox, other
    part adapted from Francis Bach who took it from Roland
    Bunschoten.

 sth * 19apr2002
 Adapted from create.m, originally written by
 (c) Stefan Harmeling, 2006

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function d = distanz(x,y,type)
0002 % DISTANZ : calculates the distances between all vectors in x and y.
0003 %
0004 % usage:
0005 %    d = distanz(x,y);
0006 %
0007 % inputs:
0008 %    x      matrix with col vectors
0009 %    y      matrix with col vectors (default == x)
0010 %    type   the type of algorithm that is used (default==3)
0011 %
0012 % outputs:
0013 %    d      distance matrix, not squared
0014 %
0015 % note:
0016 %    part of the code is inspired by dist.m of the nntoolbox, other
0017 %    part adapted from Francis Bach who took it from Roland
0018 %    Bunschoten.
0019 %
0020 % sth * 19apr2002
0021 % Adapted from create.m, originally written by
0022 % (c) Stefan Harmeling, 2006
0023 
0024 if exist('type')~=1|isempty(type), type = 3; end
0025 switch type
0026  case 1  % inspired by dist.m
0027   if exist('y')~=1|isempty(y)
0028     % here comes code just for x
0029     [rx,cx] = size(x);
0030     d = zeros(cx,cx);
0031     nuller = zeros(cx,1);
0032     for c = 1:cx
0033       d(c,:) = sum((x-x(:,c+nuller)).^2,1);
0034     end
0035   else
0036     % here comes code for x and y
0037     [rx,cx] = size(x);
0038     [ry,cy] = size(y);
0039     if rx~=ry, error('x and y do not fit'), end
0040     d = zeros(cx,cy);
0041     if cx>cy
0042       nuller = zeros(cx,1);
0043       for c = 1:cy
0044     d(:,c) = sum((x-y(:,c+nuller)).^2,1)';
0045       end
0046     else
0047       nuller = zeros(cy,1);
0048       for c = 1:cx
0049     d(c,:) = sum((x(:,c+nuller)-y).^2,1);
0050       end
0051     end
0052   end
0053  
0054  case 2  % same as case 1, but with repmat instead of nuller
0055   if exist('y')~=1|isempty(y)
0056     % here comes code just for x
0057     [rx,cx] = size(x);
0058     d = zeros(cx,cx);
0059     nuller = zeros(cx,1);
0060     for c = 1:cx
0061       d(c,:) = sum((x-repmat(x(:,c),[1 cx])).^2,1);
0062     end
0063   else
0064     % here comes code for x and y
0065     [rx,cx] = size(x);
0066     [ry,cy] = size(y);
0067     if rx~=ry, error('x and y do not fit'), end
0068     d = zeros(cx,cy);
0069     if cx>cy
0070       nuller = zeros(cx,1);
0071       for c = 1:cy
0072     d(:,c) = sum((x-repmat(y(:,c),[1 cx])).^2,1)';
0073       end
0074     else
0075       nuller = zeros(cy,1);
0076       for c = 1:cx
0077     d(c,:) = sum((repmat(x(:,c),[1 cy])-y).^2,1);
0078       end
0079     end
0080   end
0081   
0082  case 3  % inspired by Roland Bunschoten
0083   if exist('y')~=1|isempty(y)
0084     % here comes code just for x
0085     cx = size(x,2);
0086     xx = sum(x.*x,1); xz = x'*x;
0087     d = abs(repmat(xx',[1 cx]) - 2*xz + repmat(xx,[cx 1]));
0088   else
0089     % here comes code for x and y
0090     [rx,cx] = size(x);
0091     [ry,cy] = size(y);
0092     if rx~=ry, error('x and y do not fit'), end
0093     xx = sum(x.*x,1); yy = sum(y.*y,1);  xy = x'*y;  
0094     d = abs(repmat(xx',[1 cy]) + repmat(yy,[cx 1]) - 2*xy);
0095   end
0096 end
0097 
0098 d = sqrt(d);
0099

Generated on Tue 04-May-2010 16:00:20 by m2html © 2003