NORMALIZE Normalize a vector. V2 = normalize(V); Returns the normalization of vector V, such that ||V|| = 1. V can be either a row or a column vector. When V is a MxN array, normalization is performed for each row of the array. See Also: vectors2d, normalizeVector, vectorNorm
0001 function vn = normalize(v) 0002 %NORMALIZE Normalize a vector. 0003 % 0004 % V2 = normalize(V); 0005 % Returns the normalization of vector V, such that ||V|| = 1. V can be 0006 % either a row or a column vector. 0007 % 0008 % When V is a MxN array, normalization is performed for each row of the 0009 % array. 0010 % 0011 % See Also: 0012 % vectors2d, normalizeVector, vectorNorm 0013 % 0014 0015 % --------- 0016 % author : David Legland 0017 % INRA - TPV URPOI - BIA IMASTE 0018 % created the 29/11/2004. 0019 % 0020 0021 % HISTORY 0022 % 14/01/2005 : correct bug 0023 0024 0025 dim = size(v); 0026 0027 if dim(1)==1 || dim(2)==1 0028 vn = v/sqrt(sum(v.*v)); 0029 else 0030 vn = v./repmat(sqrt(sum(v.*v, 2)), [1 dim(2)]); 0031 end