# v_disteusq

## PURPOSE

V_DISTEUSQ calculate euclidean, squared euclidean or mahanalobis distance D=(X,Y,MODE,W)

## SYNOPSIS

function d=v_disteusq(x,y,mode,w)

## DESCRIPTION

    V_DISTEUSQ calculate euclidean, squared euclidean or mahanalobis distance D=(X,Y,MODE,W)

     Inputs: X,Y         Vector sets to be compared. Each row contains a data vector.
                         X and Y must have the same number of columns.

             MODE        Character string selecting the following options:
                             'x'  Calculate the full distance matrix from every row of X to every row of Y
                             'd'  Calculate only the distance between corresponding rows of X and Y
                                  The default is 'd' if X and Y have the same number of rows otherwise 'x'.
                             's'  take the square-root of the result to give the euclidean distance.

             W           Optional weighting matrix: the distance calculated is (x-y)*W*(x-y)'
                         If W is a vector, then the matrix diag(W) is used.

     Output: D           If MODE='d' then D is a column vector with the same number of rows as the shorter of X and Y.
                         If MODE='x' then D is a matrix with the same number of rows as X and the same number of columns as Y'.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function d=v_disteusq(x,y,mode,w)
    0002 %V_DISTEUSQ calculate euclidean, squared euclidean or mahanalobis distance D=(X,Y,MODE,W)
    0003 %
    0004 % Inputs: X,Y         Vector sets to be compared. Each row contains a data vector.
    0005 %                     X and Y must have the same number of columns.
    0006 %
    0007 %         MODE        Character string selecting the following options:
    0008 %                         'x'  Calculate the full distance matrix from every row of X to every row of Y
    0009 %                         'd'  Calculate only the distance between corresponding rows of X and Y
    0010 %                              The default is 'd' if X and Y have the same number of rows otherwise 'x'.
    0011 %                         's'  take the square-root of the result to give the euclidean distance.
    0012 %
    0013 %         W           Optional weighting matrix: the distance calculated is (x-y)*W*(x-y)'
    0014 %                     If W is a vector, then the matrix diag(W) is used.
    0015 %
    0016 % Output: D           If MODE='d' then D is a column vector with the same number of rows as the shorter of X and Y.
    0017 %                     If MODE='x' then D is a matrix with the same number of rows as X and the same number of columns as Y'.
    0018 %
    0019
    0020 %      Copyright (C) Mike Brookes 1998
    0021 %      Version: $Id: v_disteusq.m 10865 2018-09-21 17:22:45Z dmb $
    0022 %
    0023 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0024 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0025 %
    0026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0027 %   This program is free software; you can redistribute it and/or modify
    0028 %   it under the terms of the GNU Lesser General Public License as published by
    0029 %   the Free Software Foundation; either version 3 of the License, or
    0030 %   (at your option) any later version.
    0031 %
    0032 %   This program is distributed in the hope that it will be useful,
    0033 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0034 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0035 %   GNU Lesser General Public License for more details.
    0036 %
    0037 %   You can obtain a copy of the GNU Lesser General Public License from
    0038 %   https://www.gnu.org/licenses/ .
    0039 %
    0040 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0041
    0042 [nx,p]=size(x); ny=size(y,1);
    0043 if nargin<3 | isempty(mode) mode='0'; end
    0044 if any(mode=='d') | (mode~='x' & nx==ny)
    0045
    0046     % Do pairwise distance calculation
    0047
    0048     nx=min(nx,ny);
    0049     z=double(x(1:nx,:))-double(y(1:nx,:));
    0050     if nargin<4
    0051         d=sum(z.*conj(z),2);
    0052     elseif min(size(w))==1
    0053         wv=w(:).';
    0054         d=sum(z.*wv(ones(size(z,1),1),:).*conj(z),2);
    0055     else
    0056         d=sum(z*w.*conj(z),2);
    0057     end
    0058 else
    0059
    0060     % Calculate full distance matrix
    0061
    0062     if p>1
    0063
    0064         % x and y are matrices
    0065
    0066         if nargin<4
    0067             z=permute(double(x(:,:,ones(1,ny))),[1 3 2])-permute(double(y(:,:,ones(1,nx))),[3 1 2]);
    0068             d=sum(z.*conj(z),3);
    0069         else
    0070             nxy=nx*ny;
    0071             z=reshape(permute(double(x(:,:,ones(1,ny))),[1 3 2])-permute(double(y(:,:,ones(1,nx))),[3 1 2]),nxy,p);
    0072             if min(size(w))==1
    0073                 wv=w(:).';
    0074                 d=reshape(sum(z.*wv(ones(nxy,1),:).*conj(z),2),nx,ny);
    0075             else
    0076                 d=reshape(sum(z*w.*conj(z),2),nx,ny);
    0077             end
    0078         end
    0079     else
    0080
    0081         % x and y are vectors
    0082
    0083         z=double(x(:,ones(1,ny)))-double(y(:,ones(1,nx))).';
    0084         if nargin<4
    0085             d=z.*conj(z);
    0086         else
    0087             d=w*z.*conj(z);
    0088         end
    0089     end
    0090 end
    0091 if any(mode=='s')
    0092     d=sqrt(d);
    0093 end
    0094