Home > FMAToolbox > Analyses > Distance.m

Distance

PURPOSE ^

Distance - Compute instantaneous distance to a reference point.

SYNOPSIS ^

function distance = Distance(positions,reference,varargin)

DESCRIPTION ^

Distance - Compute instantaneous distance to a reference point.

  USAGE

    distance = Distance(positions,reference,<options>)

    positions      position <a href="matlab:help samples">samples</a>
    reference      reference point (same coordinates as positions)
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'type'        'l' if X is linear (default), 'c' if X is circular (for
                    1D positions only, which should be in [0..1])
    =========================================================================

  SEE

    See also Diff.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function distance = Distance(positions,reference,varargin)
0002 
0003 %Distance - Compute instantaneous distance to a reference point.
0004 %
0005 %  USAGE
0006 %
0007 %    distance = Distance(positions,reference,<options>)
0008 %
0009 %    positions      position <a href="matlab:help samples">samples</a>
0010 %    reference      reference point (same coordinates as positions)
0011 %    <options>      optional list of property-value pairs (see table below)
0012 %
0013 %    =========================================================================
0014 %     Properties    Values
0015 %    -------------------------------------------------------------------------
0016 %     'type'        'l' if X is linear (default), 'c' if X is circular (for
0017 %                    1D positions only, which should be in [0..1])
0018 %    =========================================================================
0019 %
0020 %  SEE
0021 %
0022 %    See also Diff.
0023 
0024 % Copyright (C) 2004-2012 by Michaƫl Zugaro
0025 %
0026 % This program is free software; you can redistribute it and/or modify
0027 % it under the terms of the GNU General Public License as published by
0028 % the Free Software Foundation; either version 3 of the License, or
0029 % (at your option) any later version.
0030 
0031 % Default values
0032 type = 'linear';
0033 
0034 if nargin < 2 | mod(length(varargin),2) ~= 0,
0035     error('Incorrect number of parameters (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0036 end
0037 if ~issamples(positions,'#1') && ~issamples(positions,'#2'),
0038     error('Incorrect positions - should be a vector (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0039 end
0040 if ~isdvector(reference,'#1') && ~isdvector(reference,'#2'),
0041     error('Incorrect reference - should be a vector (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0042 end
0043 if length(reference) ~= size(positions,2)-1,
0044     error('Positions and reference have incompatible sizes (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0045 end
0046 
0047 % Parse parameter list
0048 for j = 1:2:length(varargin),
0049     if ~ischar(varargin{j}),
0050         error(['Parameter ' num2str(j+2) ' is not a property (type ''help <a href="matlab:help Distance">Distance</a>'' for details).']);
0051     end
0052     switch(lower(varargin{j})),
0053         case 'type',
0054             type = varargin{j+1};
0055             if ~isastring(type,'l','c'),
0056                 error('Incorrect value for property ''type'' (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0057             end
0058         otherwise,
0059             error(['Unknown property ''' num2str(varargin{j}) ''' (type ''help <a href="matlab:help Distance">Distance</a>'' for details).']);
0060     end
0061 end
0062 
0063 distance = [];
0064 if isempty(positions), return; end
0065 
0066 distance = positions(:,1);
0067 
0068 if issamples(positions,'#1'),
0069     if type(1) == 'l',
0070         distance(:,2) = abs(positions(:,2)-reference(1));
0071     else
0072         % Make sure X is normalized
0073         if max(positions(:,2)) > 1 || min(positions(:,2)) < 0,
0074             positions(:,2) = ZeroToOne(positions(:,2));
0075             warning('Positions should contain values in [0 1]. The data will now be transformed accordingly.');
0076         end
0077         distance1 = abs(positions(:,2)-reference(1));
0078         distance2 = 1-abs(positions(:,2)-reference(1));
0079         distance(:,2) = min([distance1 distance2],[],2);
0080     end
0081 else
0082     distance(:,2) = sqrt((positions(:,2)-reference(1)).^2+(positions(:,3)-reference(2)).^2);
0083 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005