In MATLAB, you perform the cross product using the built-in function appropriately named cross
.
Understanding the cross
Function in MATLAB
The cross(A, B)
function computes the cross product of two arrays, A
and B
. The operation fundamentally relies on vectors having a length of 3, a key requirement for the cross product in 3D space.
Cross Product for Vectors
Key Point: As per the documentation, if A
and B
are vectors, then they must have a length of 3. This means the input vectors must represent points or directions in a 3-dimensional space.
- Syntax:
C = cross(A, B)
- Input:
A
andB
are 3-element vectors (either row or column vectors). - Output:
C
is the resulting vector, which is orthogonal to bothA
andB
.
Example:
Let's find the cross product of vector A = [1, 0, 0]
(along the x-axis) and B = [0, 1, 0]
(along the y-axis).
A = [1, 0, 0];
B = [0, 1, 0];
C = cross(A, B);
% C will be [0, 0, 1], which is along the z-axis
In this simple example, cross(A, B)
returns [0, 0, 1]
, demonstrating that the cross product of vectors along the x and y axes is a vector along the z-axis.
Cross Product for Matrices and Arrays
Key Point: If A
and B
are matrices or multidimensional arrays, then they must have the same size. In this scenario, the cross
function treats A
and B
as collections of three-element vectors.
MATLAB's cross
function calculates the cross product of corresponding 3-element vectors found within the input arrays. By default, it operates along the first dimension of size 3.
- Syntax:
C = cross(A, B)
orC = cross(A, B, dim)
- Input:
A
andB
are matrices or arrays of the same size. One dimension must have a size of 3. - Output:
C
is an array of the same size asA
andB
, containing the computed cross products.
How it Works for Matrices:
Imagine your matrix A
contains several 3D vectors stacked as rows (if dimension 2 has size 3) or columns (if dimension 1 has size 3). MATLAB pairs up the first vector from A
with the first vector from B
, computes their cross product, and stores it in the result. It repeats this for all corresponding pairs of 3-element vectors.
Example:
Let's perform a cross product on two matrices where each row is a 3D vector.
A = [1, 0, 0;
0, 1, 0;
1, 1, 0];
B = [0, 1, 0;
1, 0, 0;
0, 0, 1];
% Both matrices have size 3x3. The second dimension has size 3.
% cross will operate along dimension 2 by default if no dim is specified
% and dim 1 does not have size 3.
C = cross(A, B, 2); % Specify dimension 2 explicitly for row vectors
% C will be:
% [0, 0, 1; (cross([1,0,0], [0,1,0]))
% [0, 0, -1]; (cross([0,1,0], [1,0,0]))
% [1, -1, 0]] (cross([1,1,0], [0,0,1]))
In this example:
- The first row of
C
iscross([1, 0, 0], [0, 1, 0])
. - The second row of
C
iscross([0, 1, 0], [1, 0, 0])
. - The third row of
C
iscross([1, 1, 0], [0, 0, 1])
.
The optional dim
argument allows you to specify which dimension of the arrays contains the 3-element vectors you want to use for the cross product calculation. If dim
is not specified, MATLAB uses the first dimension of size 3. If no dimension has size 3, it uses dimension 1.
Summary of cross
Function Requirements
Input Type | Size Requirement | Vector Representation |
---|---|---|
Vectors (A , B ) |
Length must be 3 | Treated as single 3D vectors |
Matrices/Arrays | Must have same size | Treated as collections of 3-element vectors along a dimension of size 3 |
Using the cross
function correctly ensures you get the mathematically accurate cross product in 3D space, whether you're working with individual vectors or large collections of vector data.