# cross

Cross product

## Description

`C = cross(`

returns the cross product of
`A,B`

)`A`

and `B`

.

If

`A`

and`B`

are vectors, then they must have a length of 3.If

`A`

and`B`

are matrices or multidimensional arrays, then they must have the same size. In this case, the`cross`

function treats`A`

and`B`

as collections of three-element vectors. The function calculates the cross product of corresponding vectors along the first array dimension whose size equals 3.

## Examples

### Cross Product of Vectors

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

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

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 9 C(:,:,2) = 198 -164 -170 45 0 -18 -55 190 -116 C(:,:,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 10 D(:,:,2) = -37 -162 -37 50 -124 -78 1 63 118 D(:,:,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

`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 integer
scalar. The size of dimension `dim`

must be 3. If
no value is specified, the default is the first array dimension whose
size equals 3.

Consider two 2-D input arrays, `A`

and `B`

:

`cross(A,B,1)`

treats the columns of`A`

and`B`

as vectors and returns the cross products of corresponding columns.`cross(A,B,2)`

treats the rows of`A`

and`B`

as vectors and returns the cross products of corresponding rows.

`cross`

returns an error if `dim`

is
greater than `ndims(A)`

.

## More About

### Cross Product

The cross product between two 3-D vectors produces a new vector that is perpendicular to both.

Consider the two vectors

$$\begin{array}{l}A={a}_{1}\widehat{i}+{a}_{2}\widehat{j}+{a}_{3}\widehat{k}\text{\hspace{0.17em}}\text{\hspace{0.17em}},\\ B={b}_{1}\widehat{i}+{b}_{2}\widehat{j}+{b}_{3}\widehat{k}\text{\hspace{0.17em}}\text{\hspace{0.17em}}.\end{array}$$

In terms of a matrix determinant involving the basis vectors $$\widehat{i}$$, $$\widehat{j}$$,
and $$\widehat{k}$$,
the cross product of *A* and *B* is

$$\begin{array}{l}C=A\times B=\left|\begin{array}{cc}\begin{array}{cc}{\widehat{i}}_{}& {\widehat{j}}_{}\end{array}& {\widehat{k}}_{}\\ \begin{array}{cc}\begin{array}{c}{a}_{1}\\ {b}_{1}\end{array}& \begin{array}{c}{a}_{2}\\ {b}_{2}\end{array}\end{array}& \begin{array}{c}{a}_{3}\\ {b}_{3}\end{array}\end{array}\right|\\ \\ \text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}=({a}_{2}{b}_{3}-{a}_{3}{b}_{2})\widehat{i}+({a}_{3}{b}_{1}-{a}_{1}{b}_{3})\widehat{j}+({a}_{1}{b}_{2}-{a}_{2}{b}_{1})\widehat{k}\text{\hspace{0.17em}}\text{\hspace{0.17em}}.\end{array}$$

Geometrically, $$A\times B$$ is
perpendicular to both *A* and *B*.
The magnitude of the cross product, $$\Vert A\times B\Vert $$,
is equal to the area of the parallelogram formed using *A* and *B* as
sides. This area is related to the magnitudes of *A* and *B* as
well as the angle between the vectors by

$$\Vert A\times B\Vert =\Vert A\Vert \text{\hspace{0.17em}}\Vert B\Vert \mathrm{sin}\alpha \text{\hspace{0.17em}}\text{\hspace{0.17em}}.$$

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™.

This function fully supports GPU arrays. 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**

## 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:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### 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
- United Kingdom (English)