NORMALIZEVECTOR Normalize a vector to have norm equal to 1. V2 = normalizeVector(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 M-by-N array, normalization is performed for each row of the array. When V is a M-by-N-by-2 array, normalization is performed along the last dimension of the array. Example: vn = normalizeVector([3 4]) vn = 0.6000 0.8000 vectorNorm(vn) ans = 1 See also vectors2d, vectorNorm
0001 function vn = normalizeVector(v) 0002 %NORMALIZEVECTOR Normalize a vector to have norm equal to 1. 0003 % 0004 % V2 = normalizeVector(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 M-by-N array, normalization is performed for each row of 0009 % the array. 0010 % 0011 % When V is a M-by-N-by-2 array, normalization is performed along the 0012 % last dimension of the array. 0013 % 0014 % Example: 0015 % vn = normalizeVector([3 4]) 0016 % vn = 0017 % 0.6000 0.8000 0018 % vectorNorm(vn) 0019 % ans = 0020 % 1 0021 % 0022 % See also 0023 % vectors2d, vectorNorm 0024 % 0025 0026 % ------ 0027 % Author: David Legland 0028 % E-mail: david.legland@inrae.fr 0029 % Created: 2004-11-29 0030 % Copyright 2004-2024 INRA - TPV URPOI - BIA IMASTE 0031 0032 if ismatrix(v) 0033 vn = bsxfun(@rdivide, v, sqrt(sum(v.^2, 2))); 0034 else 0035 vn = bsxfun(@rdivide, v, sqrt(sum(v.^2, ndims(v)))); 0036 end