Home > matGeom > geom3d > crossProduct3d.m

crossProduct3d

PURPOSE ^

CROSSPRODUCT3D Vector cross product faster than inbuilt MATLAB cross.

SYNOPSIS ^

function c = crossProduct3d(a,b)

DESCRIPTION ^

CROSSPRODUCT3D Vector cross product faster than inbuilt MATLAB cross.

   C = crossProduct3d(A, B) 
   returns the cross product of the two 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];
     crossProduct3d(v1, v2)
     ans =
         0   0   6

   See also
   vectorAngle3d

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function c = crossProduct3d(a,b)
0002 %CROSSPRODUCT3D Vector cross product faster than inbuilt MATLAB cross.
0003 %
0004 %   C = crossProduct3d(A, B)
0005 %   returns the cross product of the two 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 %     crossProduct3d(v1, v2)
0015 %     ans =
0016 %         0   0   6
0017 %
0018 %   See also
0019 %   vectorAngle3d
0020 
0021 % ------
0022 % Author: Sven Holcombe
0023 % E-mail: N/A
0024 % Created: 2011
0025 % Copyright 2011-2024
0026 
0027 % size of inputs
0028 sizeA = size(a);
0029 sizeB = size(b);
0030 
0031 % Initialise c to the size of a or b, whichever has more dimensions. If
0032 % they have the same dimensions, initialise to the larger of the two
0033 switch sign(numel(sizeA) - numel(sizeB))
0034     case 1
0035         c = zeros(sizeA);
0036     case -1
0037         c = zeros(sizeB);
0038     otherwise
0039         c = zeros(max(sizeA, sizeB));
0040 end
0041 
0042 c(:) = bsxfun(@times, a(:,[2 3 1],:), b(:,[3 1 2],:)) - ...
0043        bsxfun(@times, b(:,[2 3 1],:), a(:,[3 1 2],:));

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022