-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcross3.m
39 lines (33 loc) · 1.13 KB
/
cross3.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function c = cross3(a, b)
% CROSS3 Cross product of two (sets of) vectors
%
% Crosses each column of the first input with the corresponding column of
% the second input, where each input is 3-by-n. This is a little better in
% generated C code than 'cross' and is specialized for 3-dimensional
% vectors.
%
% c = CROSS3(a, b)
%
% Inputs:
%
% a Left-hand-side vectors (3-by-n)
% b Right-hand-side vectors (3-by-n)
%
% Outputs:
%
% c Cross product of columns of a with corresponding columns of b (3-by-n)
% Copyright 2016 An Uncommon Lab
%#codegen
% Check dimensions.
assert(size(a, 1) == 3 && size(b, 1) == 3, ...
'%s: The vectors must be 3-by-n.', mfilename);
assert( size(a, 2) == size(b, 2) ...
|| size(a, 2) == 1 ...
|| size(b, 2) == 1, ...
['%s: The number of input left-hand-side vectors must ' ...
'match the number of input right-hand-side vectors ' ...
'(or either can be 3-by-1).'], mfilename);
c = [a(2,:) .* b(3,:) - a(3,:) .* b(2,:); ...
a(3,:) .* b(1,:) - a(1,:) .* b(3,:); ...
a(1,:) .* b(2,:) - a(2,:) .* b(1,:)];
end % cross3