cross - Cross product - MATLAB (2024)

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

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.

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,BInput arrays
numeric arrays

Input arrays, specified as numeric arrays.

Data Types: single | double
Complex Number Support: Yes

dimDimension 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 columnsof A and B as vectors and returnsthe cross products of corresponding columns.

  • cross(A,B,2) treats the rows of A and B asvectors and returns the cross products of corresponding rows.

cross - Cross product - MATLAB (1)

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

A=a1i^+a2j^+a3k^,B=b1i^+b2j^+b3k^.

In terms of a matrix determinant involving the basis vectors i^, j^,and k^,the cross product of A and B is

C=A×B=|i^j^k^a1b1a2b2a3b3|=(a2b3a3b2)i^+(a3b1a1b3)j^+(a1b2a2b1)k^.

Geometrically, A×B isperpendicular to both A and B.The magnitude of the cross product, A×B,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

A×B=ABsinα.

Thus, if A and B are parallel,then the cross product is zero.

cross - Cross product - MATLAB (2)

Extended Capabilities

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

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.

cross - Cross product - MATLAB (3)

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)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

cross - Cross product - MATLAB (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5693

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.