ISPERPENDICULAR Check orthogonality of two vectors. B = isPerpendicular(V1, V2) where V1 and V2 are two 1-by-2 row arrays, returns 1 if the vectors are perpendicular, and 0 otherwise. Also works when V1 and V2 are two N-by-2 arrays with same number of rows. In this case, return a N-by-1 array containing 1 at the positions of perpendicular vectors. Also works when one of V1 or V2 is 1-by-2 and the other one is a N-by-2 array. In this case the result has size N-by-1. B = isPerpendicular(V1, V2, ACCURACY) specifies accuracy of numerical tests, default is 1e-14. Example isPerpendicular([1 2 1], [2 4 2]) ans = 1 isPerpendicular([1 2 1], [1 3 2]) ans = 0 See also vectors2d, isParallel, lines2d
0001 function b = isPerpendicular(v1, v2, varargin) 0002 %ISPERPENDICULAR Check orthogonality of two vectors. 0003 % 0004 % B = isPerpendicular(V1, V2) 0005 % where V1 and V2 are two 1-by-2 row arrays, returns 1 if the vectors are 0006 % perpendicular, and 0 otherwise. 0007 % 0008 % Also works when V1 and V2 are two N-by-2 arrays with same number of 0009 % rows. In this case, return a N-by-1 array containing 1 at the positions 0010 % of perpendicular vectors. 0011 % 0012 % Also works when one of V1 or V2 is 1-by-2 and the other one is a N-by-2 0013 % array. In this case the result has size N-by-1. 0014 % 0015 % B = isPerpendicular(V1, V2, ACCURACY) 0016 % specifies accuracy of numerical tests, default is 1e-14. 0017 % 0018 % 0019 % Example 0020 % isPerpendicular([1 2 1], [2 4 2]) 0021 % ans = 0022 % 1 0023 % 0024 % isPerpendicular([1 2 1], [1 3 2]) 0025 % ans = 0026 % 0 0027 % 0028 % See also 0029 % vectors2d, isParallel, lines2d 0030 % 0031 0032 % ------ 0033 % Author: David Legland 0034 % e-mail: david.legland@inra.fr 0035 % Created: 2006-04-25 0036 % Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0037 0038 % HISTORY 0039 % 2007-09-18 copy from isPerpendicular, adapt to any dimension, and add 0040 % psb to specify precision 0041 % 2009-09-21 fix bug for array of 3 vectors 0042 % 2011-01-20 replace repmat by ones-indexing (faster) 0043 % 2017-08-31 use normalized vectors 0044 0045 % default accuracy 0046 acc = 1e-14; 0047 if ~isempty(varargin) 0048 acc = abs(varargin{1}); 0049 end 0050 0051 % normalize vectors 0052 v1 = normalizeVector(v1); 0053 v2 = normalizeVector(v2); 0054 0055 % adapt size of inputs 0056 n1 = size(v1, 1); 0057 n2 = size(v2, 1); 0058 if n1 ~= n2 0059 if n1 == 1 0060 v1 = v1(ones(n2, 1), :); 0061 elseif n2==1 0062 v2 = v2(ones(n1, 1), :); 0063 else 0064 error('Inputs must either have same size, or one must be scalar'); 0065 end 0066 end 0067 0068 % performs test 0069 b = abs(dot(v1, v2, 2)) < acc;