Cross product
collapse all in page
Syntax
C = cross(A,B)
C = cross(A,B,dim)
Description
C = cross(A,B)
returns the cross product of A
and B
.
If
A
andB
are vectors, then they must have a length of 3.If
A
andB
are matrices or multidimensional arrays, then they must have the same size. In this case, thecross
function treatsA
andB
as collections of three-element vectors. The function calculates the cross product of corresponding vectors along the first array dimension whose size equals 3.
example
C = cross(A,B,dim)
evaluatesthe cross product of arrays A
and B
alongdimension, dim
. A
and B
musthave the same size, and both size(A,dim)
and size(B,dim)
mustbe 3. The dim
input is a positive integer scalar.
example
Examples
collapse all
Cross Product of Vectors
Open Live Script
Create two 3-D vectors.
A = [4 -2 1];B = [1 -1 3];
Find the cross product of A
and B
. The result, C
, is a vector that is perpendicular to both A
and B
.
C = cross(A,B)
C = 1×3 -5 -11 -2
Use dot products to verify that C
is perpendicular to A
and B
.
dot(C,A)==0 & dot(C,B)==0
ans = logical 1
The result is logical 1
(true
).
Cross Product of Matrices
Open Live Script
Create two matrices containing random integers.
A = randi(15,3,5)
A = 3×5 13 14 5 15 15 14 10 9 3 8 2 2 15 15 13
B = randi(25,3,5)
B = 3×5 4 20 1 17 10 11 24 22 19 17 23 17 24 19 5
Find the cross product of A
and B
.
C = cross(A,B)
C = 3×5 300 122 -114 -228 -181 -291 -198 -105 -30 55 87 136 101 234 175
The result, C
, contains five independent cross products between the columns of A
and B
. For example, C(:,1)
is equal to the cross product of A(:,1)
with B(:,1)
.
Cross Product of Multidimensional Arrays
Open Live Script
Create two 3-by-3-by-3 multidimensional arrays of random integers.
A = randi(10,3,3,3);B = randi(25,3,3,3);
Find the cross product of A
and B
, treating the rows as vectors.
C = cross(A,B,2)
C = C(:,:,1) = -34 12 62 15 72 -109 -49 8 9C(:,:,2) = 198 -164 -170 45 0 -18 -55 190 -116C(:,:,3) = -109 -45 131 1 -74 82 -6 101 -121
The result is a collection of row vectors. For example, C(1,:,1)
is equal to the cross product of A(1,:,1)
with B(1,:,1)
.
Find the cross product of A
and B
along the third dimension (dim = 3
).
D = cross(A,B,3)
D = D(:,:,1) = -14 179 -106 -56 -4 -75 2 -37 10D(:,:,2) = -37 -162 -37 50 -124 -78 1 63 118D(:,:,3) = 62 -170 56 46 72 105 -2 -53 -160
The result is a collection of vectors oriented in the third dimension. For example, D(1,1,:)
is equal to the cross product of A(1,1,:)
with B(1,1,:)
.
Input Arguments
collapse all
A,B
— Input arrays
numeric arrays
Input arrays, specified as numeric arrays.
Data Types: single
| double
Complex Number Support: Yes
dim
— Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integerscalar. The size of dimension dim
must be 3. Ifno value is specified, the default is the first array dimension whosesize equals 3.
Consider two 2-D input arrays, A
and B
:
cross(A,B,1)
treats the columnsofA
andB
as vectors and returnsthe cross products of corresponding columns.cross(A,B,2)
treats the rows ofA
andB
asvectors and returns the cross products of corresponding rows.
cross
returns an error if dim
isgreater than ndims(A)
.
More About
collapse all
Cross Product
The cross product between two 3-D vectors producesa new vector that is perpendicular to both.
Consider the two vectors
In terms of a matrix determinant involving the basis vectors , ,and ,the cross product of A and B is
Geometrically, isperpendicular to both A and B.The magnitude of the cross product, ,is equal to the area of the parallelogram formed using A and B assides. This area is related to the magnitudes of A and B aswell as the angle between the vectors by
Thus, if A and B are parallel,then the cross product is zero.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If supplied,
dim
must be a constant.See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
Code generation does not support sparse matrix inputs for this function.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
If supplied,
dim
must be a constant.Code generation does not support sparse matrix inputs for this function.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The cross
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
See Also
dot | kron | norm
External Websites
- Applied Linear Algebra (MathWorks Teaching Resources)
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office