VECTORCROSS3D Vector cross product faster than inbuilt MATLAB cross. C = vectorCross3d(A, B) returns the cross product of the 3D vectors A and B, that is: C = A x B A and B must be N-by-3 element vectors. If either A or B is a 1-by-3 row vector, the result C will have the size of the other input and will be the concatenation of each row's cross product. Example v1 = [2 0 0]; v2 = [0 3 0]; vectorCross3d(v1, v2) ans = 0 0 6 Class support for inputs A,B: float: double, single See also DOT.
0001 function c = vectorCross3d(a,b) 0002 %VECTORCROSS3D Vector cross product faster than inbuilt MATLAB cross. 0003 % 0004 % C = vectorCross3d(A, B) 0005 % returns the cross product of the 3D vectors A and B, that is: 0006 % C = A x B 0007 % A and B must be N-by-3 element vectors. If either A or B is a 1-by-3 0008 % row vector, the result C will have the size of the other input and will 0009 % be the concatenation of each row's cross product. 0010 % 0011 % Example 0012 % v1 = [2 0 0]; 0013 % v2 = [0 3 0]; 0014 % vectorCross3d(v1, v2) 0015 % ans = 0016 % 0 0 6 0017 % 0018 % 0019 % Class support for inputs A,B: 0020 % float: double, single 0021 % 0022 % See also DOT. 0023 0024 % Sven Holcombe 0025 0026 % needed_colons = max([3, length(size(a)), length(size(b))]) - 3; 0027 % tmp_colon = {':'}; 0028 % clnSet = tmp_colon(ones(1, needed_colons)); 0029 % 0030 % c = bsxfun(@times, a(:,[2 3 1],clnSet{:}), b(:,[3 1 2],clnSet{:})) - ... 0031 % bsxfun(@times, b(:,[2 3 1],clnSet{:}), a(:,[3 1 2],clnSet{:})); 0032 0033 % deprecation warning 0034 warning('geom3d:deprecated', ... 0035 [mfilename ' is deprecated, use ''crossProduct3d'' instead']); 0036 0037 sza = size(a); 0038 szb = size(b); 0039 0040 % Initialise c to the size of a or b, whichever has more dimensions. If 0041 % they have the same dimensions, initialise to the larger of the two 0042 switch sign(numel(sza) - numel(szb)) 0043 case 1 0044 c = zeros(sza); 0045 case -1 0046 c = zeros(szb); 0047 otherwise 0048 c = zeros(max(sza, szb)); 0049 end 0050 0051 c(:) = bsxfun(@times, a(:,[2 3 1],:), b(:,[3 1 2],:)) - ... 0052 bsxfun(@times, b(:,[2 3 1],:), a(:,[3 1 2],:));