ISPERPENDICULAR3D Check orthogonality of two 3D vectors. B = isPerpendicular3d(V1, V2) where V1 and V2 are 2 [1x3] arrays, returns 1 if the vectors are orthogonal, and 0 otherwise. Also works when V1 and V2 are two [Nx3] arrays with same number of rows. In this case, return a [Nx1] array containing 1 at the positions of parallel vectors. Also works when one of V1 or V2 is scalar and the other one is [Nx3] array, in this case return [Nx1] results. B = isPerpendicular3d(V1, V2, TOL) Specifies the absolute tolerance (default is 1e-14). Example isPerpendicular3d([1 0 0], [0 1 0]) ans = 1 isPerpendicular3d([1 0 1], [1 0 0]) ans = 0 See also vectors3d, isParallel3d ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2006-04-25 Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).
0001 function b = isPerpendicular3d(v1, v2, varargin) 0002 %ISPERPENDICULAR3D Check orthogonality of two 3D vectors. 0003 % 0004 % B = isPerpendicular3d(V1, V2) 0005 % where V1 and V2 are 2 [1x3] arrays, returns 1 if the vectors are 0006 % orthogonal, and 0 otherwise. 0007 % 0008 % Also works when V1 and V2 are two [Nx3] arrays with same number of 0009 % rows. In this case, return a [Nx1] array containing 1 at the positions 0010 % of parallel vectors. 0011 % 0012 % Also works when one of V1 or V2 is scalar and the other one is [Nx3] 0013 % array, in this case return [Nx1] results. 0014 % 0015 % B = isPerpendicular3d(V1, V2, TOL) 0016 % Specifies the absolute tolerance (default is 1e-14). 0017 % 0018 % 0019 % Example 0020 % isPerpendicular3d([1 0 0], [0 1 0]) 0021 % ans = 0022 % 1 0023 % 0024 % isPerpendicular3d([1 0 1], [1 0 0]) 0025 % ans = 0026 % 0 0027 % 0028 % See also 0029 % vectors3d, isParallel3d 0030 % 0031 % ------ 0032 % Author: David Legland 0033 % e-mail: david.legland@grignon.inra.fr 0034 % Created: 2006-04-25 0035 % Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0036 0037 % 2011.03.20 add support for tolerance, fix computation 0038 0039 % check if tolerance is specified 0040 tol = 1e-14; 0041 if ~isempty(varargin) 0042 tol = varargin{1}; 0043 end 0044 0045 % compute perpendicularity test 0046 b = abs(sum(bsxfun(@times, v1, v2), 2)) < tol;