Home > vbmeg > functions > brain > vb_sq_distance.m

vb_sq_distance

PURPOSE ^

square distance matrix for X and Y

SYNOPSIS ^

function dd = vb_sq_distance(X,Y,flg)

DESCRIPTION ^

 square distance matrix for X and Y
 case 1:
  dd = vb_sq_distance(X,Y)
  dd = vb_sq_distance(X,Y,flg)
       flg = 0
  X : (D x M) matrix 
  Y : (D x N) matrix
 case 2: (default)
  dd = vb_sq_distance(X,Y,flg)
       flg = 1
  X : (M x D) matrix 
  Y : (N x D) matrix

  dd = (X-Y)^2 = X^2 + Y^2 - 2*X*Y : (M x N) square distance matrix
  D : space dimension of X and Y
  M, N : number of points in X and Y
  

   2013-7-05 Masa-aki Sato 

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function dd = vb_sq_distance(X,Y,flg)
0002 % square distance matrix for X and Y
0003 % case 1:
0004 %  dd = vb_sq_distance(X,Y)
0005 %  dd = vb_sq_distance(X,Y,flg)
0006 %       flg = 0
0007 %  X : (D x M) matrix
0008 %  Y : (D x N) matrix
0009 % case 2: (default)
0010 %  dd = vb_sq_distance(X,Y,flg)
0011 %       flg = 1
0012 %  X : (M x D) matrix
0013 %  Y : (N x D) matrix
0014 %
0015 %  dd = (X-Y)^2 = X^2 + Y^2 - 2*X*Y : (M x N) square distance matrix
0016 %  D : space dimension of X and Y
0017 %  M, N : number of points in X and Y
0018 %
0019 %
0020 %   2013-7-05 Masa-aki Sato
0021 %
0022 % Copyright (C) 2011, ATR All Rights Reserved.
0023 % License : New BSD License(see VBMEG_LICENSE.txt)
0024 
0025 if nargin<3, flg=1;end
0026 
0027 if flg==0
0028     if (size(X,1) ~= size(Y,1))
0029        error('dimension of X and Y are different ');
0030     end
0031     
0032     XX = sum(X.^2,1); % 1 x M
0033     YY = sum(Y.^2,1); % 1 x N
0034     XY = -2*X'*Y; % M x N
0035     
0036     %  (X-Y)^2 = X^2 + Y^2 - 2*X'*Y
0037     dd = vb_repadd( XY, XX');
0038     dd = vb_repadd( dd, YY);
0039 else
0040     if (size(X,2) ~= size(Y,2))
0041        error('dimension of X and Y are different ');
0042     end
0043     
0044     XX = sum(X.^2,2); % M x 1
0045     YY = sum(Y.^2,2); % N x 1
0046     XY = -2*X*Y'; % M x N
0047     
0048     %  (X-Y)^2 = X^2 + Y^2 - 2*X*Y'
0049     dd = vb_repadd( XY, XX);
0050     dd = vb_repadd( dd, YY');
0051 end
0052 
0053 dd = max(dd,0);

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005