vector_magnitude - computes the magnitude of a 3D Cartesian vector vMagnitude = vector_magnitude(v,[origin]) v - an Mx3 vector with M rows of (Vx,Vy,Vz) components origin - the coordinate system origin, a 1x3 vector. This argument is optional, the default is (0,0,0) vMagnitude - Mx1 column vector of magnitudes for each row vector in v, ie: sqrt( (Vx-xo).^2 + (Vy-yo).^2 + (Vz-zo).^2 ); The matlab function 'norm' does not give this (as at v6.5)
0001 function vMagnitude = vector_magnitude(v,origin) 0002 0003 % vector_magnitude - computes the magnitude of a 3D Cartesian vector 0004 % 0005 % vMagnitude = vector_magnitude(v,[origin]) 0006 % 0007 % v - an Mx3 vector with M rows of (Vx,Vy,Vz) components 0008 % 0009 % origin - the coordinate system origin, a 1x3 vector. 0010 % This argument is optional, the default is (0,0,0) 0011 % 0012 % vMagnitude - Mx1 column vector of magnitudes for each row 0013 % vector in v, ie: 0014 % 0015 % sqrt( (Vx-xo).^2 + (Vy-yo).^2 + (Vz-zo).^2 ); 0016 % 0017 % The matlab function 'norm' does not give this (as at v6.5) 0018 % 0019 0020 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:36 $ 0021 0022 % Licence: GNU GPL, no express or implied warranties 0023 % History: 7/29/1999, Tom Ferree at EGI 0024 % 08/2003, Darren.Weber_at_radiology.ucsf.edu 0025 % adapted the function to handle an offset origin 0026 % and explicit use of a matrix v 0027 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0028 0029 if ~exist('origin','var'), origin = [ 0, 0, 0 ]; end 0030 if isempty(origin), origin = [ 0, 0, 0 ]; end 0031 0032 if ~exist('v','var'), error('no input 3D Cartesian vector'); end 0033 if isempty(v), error('empty input 3D Cartesian vector'); end 0034 0035 Vx = v(:,1) - origin(1); 0036 Vy = v(:,2) - origin(2); 0037 Vz = v(:,3) - origin(3); 0038 0039 vMagnitude = sqrt( Vx.^2 + Vy.^2 + Vz.^2 ); 0040 0041 return