Skip to main content
v2026.11,986 entries · CC-BY 4.0

Cross Product in MATLAB: cross() Syntax, dim, and Errors

How to compute a cross product in MATLAB with cross(A,B) and cross(A,B,dim), check it with dot, and fix the size-3 error.

Written and maintained by CASRAI Editorial Board

Published

On this page

To compute a cross product in MATLAB, call C = cross(A,B) with two inputs that have a dimension of size 3. For two 3-element vectors the result is a 3-element vector perpendicular to both. This guide covers the syntax, how the optional dim argument chooses which dimension is treated as the vectors, how to check the result, and why the size-3 error appears. Facts about cross come from the MathWorks documentation page for the function, read 2026-11-08.

What the MATLAB cross product is used for in research

The cross product of two 3-D vectors is a third vector perpendicular to both. Researchers use it wherever a normal direction or an oriented area is needed from two direction vectors, for example the normal of a surface patch in a mesh, torque from a position and a force vector, or the axis perpendicular to two measured directions. MATLAB’s cross does this for a single pair of vectors or for many pairs stored in a matrix or N-D array at once.

Install or access (official source only)

cross is a built-in MATLAB function, documented as introduced before R2006a. The documentation page does not list a toolbox for basic use; optional toolboxes appear only for specific features (MATLAB Coder for C/C++ code generation, GPU Coder for CUDA code generation, Parallel Computing Toolbox for GPU arrays and distributed arrays). MATLAB itself is obtained from MathWorks at mathworks.com/products/matlab.html; many universities provide campus licences, so check with your institution’s IT service first. For a free route, see the Octave section below.

Step-by-step: cross product in MATLAB

Cross product of two vectors

  1. Define two vectors with exactly three elements each: A = [4 -2 1]; B = [1 -1 3];
  2. Call the function: C = cross(A,B)
  3. Read the result: C = 1×3 with values -5 -11 -2.
  4. Check perpendicularity: dot(C,A)==0 & dot(C,B)==0 returns logical 1, because both dot products are zero.

You can verify the numbers by hand with the component formula (a2b3 − a3b2, a3b1 − a1b3, a1b2 − a2b1): (-2)(3) − (1)(-1) = -5; (1)(1) − (4)(3) = -11; (4)(-1) − (-2)(1) = -2.

Cross products of many vectors at once

If A and B are matrices of the same size, cross(A,B) treats each column as a vector when the first dimension has size 3. For two 3-by-5 matrices the result is 3-by-5, and column k of the result equals cross(A(:,k),B(:,k)). Without dim, MATLAB uses the first dimension whose size is 3.

Choosing the dimension with dim

The syntax C = cross(A,B,dim) operates along dimension dim, a positive integer scalar. A and B must be the same size and the size of dimension dim must be 3. For a 3-by-3-by-3 array, cross(A,B,2) computes row-wise products and cross(A,B,3) computes products along the third dimension. If dim is greater than ndims(A) the function returns an error.

Input size (A and B) Dimension used with no dim What is treated as a vector To use the other orientation
3-by-N (N not 3) 1 Each column Not possible: dimension 2 is not size 3
N-by-3 (N not 3) 2 (first size-3 dimension) Each row Transpose, or use cross(A,B,2) explicitly
3-by-3 1 (first size-3 dimension) Each column cross(A,B,2) for rows
3-by-3-by-3 1 Each column of each page cross(A,B,2) or cross(A,B,3)

The table follows from the documented default (the first dimension of size 3) and the documented requirement that the chosen dimension has size 3. The square 3-by-3 case is the one that surprises people: if your vectors are stored as rows, pass 2 explicitly. For background on how MATLAB shapes and indexes arrays, see matrices in MATLAB and MATLAB indexing.

Output and how to read it

  • Vector inputs: C is a 3-element vector perpendicular to A and B. Its direction follows the right-hand rule and swapping the inputs flips its sign, so cross(B,A) equals -cross(A,B).
  • Array inputs: C has the same size as the inputs and holds the cross products of corresponding 3-element vectors along the chosen dimension.
  • Data types: inputs are numeric single or double; complex values are supported.
  • Zero result: a result of [0 0 0] means the two vectors are parallel (or one is zero), so no unique perpendicular direction exists.

The perpendicularity check with dot in the example above is a quick sanity test after any change to how your data is arranged. With floating-point data, compare against a small tolerance (for example abs(dot(C,A)) < 1e-10) instead of testing for exact equality.

Troubleshooting

Error: A and B must have at least one dimension of length 3

This message is reported in MathWorks Answers threads when neither input has a dimension of size 3, for example a 25000-by-1 column of values. cross only works on 3-element vectors, so the data must be reshaped into an array that has a size-3 dimension (for example 25000-by-3 or 3-by-25000) before the call. Check size(A) first.

Wrong rows or columns are multiplied

Without dim, the first size-3 dimension wins. For a 3-by-3 matrix that is always the columns. Pass dim explicitly, as in cross(A,B,2), whenever your vectors are rows and the matrix could be 3-by-3.

Error when dim is too large

The documentation states that cross errors if dim is greater than ndims(A). Use a dim from 1 up to ndims(A) whose size is 3.

Inputs of different size

For matrices and N-D arrays, A and B must be the same size.

Free alternatives

Tool Function Documented behaviour
GNU Octave cross(x,y), cross(x,y,dim) Computes the vector cross product of two 3-dimensional vectors. For arrays it works along the first dimension with three elements; dim forces the dimension and errors if that dimension is not size 3. For complex output, dot(conj(z),x) is zero but dot(z,x) is not.
Python with NumPy numpy.cross See the CASRAI guide to the NumPy cross product.

The Octave caveat matters if you port complex-valued code: check perpendicularity with the conjugate in Octave. Octave’s function is described in the GNU Octave Utility Functions documentation, read 2026-11-08.

FAQ

How do I do a cross product in MATLAB?

Use C = cross(A,B) with two 3-element vectors, or C = cross(A,B,dim) to choose the dimension for arrays. For A = [4 -2 1] and B = [1 -1 3] the result is [-5 -11 -2].

Does the MATLAB cross product work on vectors that are not length 3?

No. For vectors the documentation requires a length of 3, and for arrays the operating dimension must have size 3.

What is the difference between cross and dot in MATLAB?

cross returns a vector perpendicular to its inputs; dot returns a scalar for vector inputs. Computing dot of the cross product with either input should give 0.

Can I take the cross product of rows instead of columns?

Yes. Pass 2 as the third argument, cross(A,B,2), when each row holds one vector.

Which MATLAB toolbox do I need for cross?

None for basic use according to the function’s documentation page; toolboxes are listed only for code generation and GPU or distributed arrays.

Sources

  • MathWorks, cross: Cross product (MATLAB documentation), read 2026-11-08.
  • GNU Octave, Utility Functions, read 2026-11-08.
  • MathWorks Answers threads on the “at least one dimension of length 3” error, checked 2026-11-08.

Related CASRAI guides: MATLAB numel, MATLAB diff.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Cross Product in MATLAB: cross() Syntax, dim, and Errors

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →