Home > matGeom > geom2d > vectorNorm.m

vectorNorm

PURPOSE ^

Compute norm of a vector, or of a set of vectors.

SYNOPSIS ^

function n = vectorNorm(v, varargin)

DESCRIPTION ^

 Compute norm of a vector, or of a set of vectors.

   N = vectorNorm(V);
   Returns the euclidean norm of vector V.

   N = vectorNorm(V, N);
   Specifies the norm to use. N can be any value greater than 0. 
   N=1 -> city lock norm
   N=2 -> euclidean norm
   N=inf -> compute max coord.

   When V is a MxN array, compute norm for each vector of the array.
   Vector are given as rows. Result is then a [M*1] array.

   Example
   n1 = vectorNorm([3 4])
   n1 =
       5

   n2 = vectorNorm([1, 10], inf)
   n2 =
       10

   See Also:
     vectors2d, vectorAngle

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function n = vectorNorm(v, varargin)
0002 % Compute norm of a vector, or of a set of vectors.
0003 %
0004 %   N = vectorNorm(V);
0005 %   Returns the euclidean norm of vector V.
0006 %
0007 %   N = vectorNorm(V, N);
0008 %   Specifies the norm to use. N can be any value greater than 0.
0009 %   N=1 -> city lock norm
0010 %   N=2 -> euclidean norm
0011 %   N=inf -> compute max coord.
0012 %
0013 %   When V is a MxN array, compute norm for each vector of the array.
0014 %   Vector are given as rows. Result is then a [M*1] array.
0015 %
0016 %   Example
0017 %   n1 = vectorNorm([3 4])
0018 %   n1 =
0019 %       5
0020 %
0021 %   n2 = vectorNorm([1, 10], inf)
0022 %   n2 =
0023 %       10
0024 %
0025 %   See Also:
0026 %     vectors2d, vectorAngle
0027 %
0028 
0029 %   ---------
0030 %   author : David Legland
0031 %   INRA - TPV URPOI - BIA IMASTE
0032 %   created the 21/02/2005.
0033 %
0034 
0035 %   HISTORY
0036 %   02/05/2006 manage several norms
0037 %   18/09/2007 use 'isempty'
0038 %   15/10/2008 add comments
0039 %   22/05/2009 rename as vectorNorm
0040 %   01/03/2010 fix bug for inf norm
0041 %   03/01/2020 make it work for more dimensions
0042 
0043 % extract the type of norm to compute
0044 d = 2;
0045 if ~isempty(varargin)
0046     d = varargin{1};
0047 end
0048 
0049 if d==2
0050     % euclidean norm: sum of squared coordinates, and take square root
0051     n = sqrt(sum(v.*v, ndims(v)));
0052     
0053 elseif d==1 
0054     % absolute norm: sum of absolute coordinates
0055     n = sum(abs(v), ndims(v));
0056 
0057 elseif d==inf
0058     % infinite norm: uses the maximal corodinate
0059     n = max(v, [], ndims(v));
0060 
0061 else
0062     % Other norms, use explicit but slower expression
0063     n = power(sum(power(v, d), ndims(v)), 1/d);
0064     
0065 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019