Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Matrix Multiplication in MATLAB: * Operator, mtimes and pagemtimes

Multiply matrices in MATLAB with * (inner dimensions must match), .* for element-wise, and pagemtimes for N-D arrays, with worked examples.

Written and maintained by CASRAI Editorial Board

Last updated

To multiply two matrices in MATLAB, use the * operator: C = A*B. It performs linear-algebra matrix multiplication, so the number of columns of A must equal the number of rows of B. If A is m-by-p and B is p-by-n, C is m-by-n. For element-by-element multiplication use .* instead; for stacks of matrices in N-D arrays use pagemtimes. This guide covers all three, with worked 1×2, 2×2 and 2×3-by-3×3 examples you can check by hand.

What matrix multiplication is used for in research

Matrix products appear wherever data are combined linearly: applying a transformation to a set of coordinates, computing a regression fit as a design matrix times a coefficient vector, propagating a state through a model, or combining weights with features. In each case the question is the same: are the shapes compatible, and did you want a matrix product or an element-wise one? The sections below answer both. For indexing the rows and columns you multiply, see MATLAB indexing.

Install or access MATLAB

MATLAB is a MathWorks product. Installation steps are in installing MATLAB, and licence types are explained in MATLAB licensing. Everything on this page uses core MATLAB functions and operators: *, .*, mtimes and pagemtimes.

Step by step: multiply two matrices with the * operator

  1. Define the matrices, for example A = [1 3; 2 4] and B = [3 0; 1 5].
  2. Check sizes with size(A) and size(B). The columns of A must equal the rows of B. Inputs to * must be 2-D.
  3. Compute C = A*B.
  4. Read C: each entry C(i,j) is the inner product of row i of A and column j of B.

C = mtimes(A,B) is the same operation written as a function call, which exists so that the operator can be overloaded for custom classes; MathWorks notes it is rarely used directly.

Matrix multiply in MATLAB, 2×2 example

A = [1 3; 2 4];
B = [3 0; 1 5];
A*B
ans =
     6    15
    10    20

Check one entry by hand: C(1,1) = 1*3 + 3*1 = 6.

Multiply a 1×2 row vector by a matrix

x = [1 2];
M = [3 4; 5 6];
x*M
ans =
    13    16

A 1×2 times a 2×2 is 1×2: 1*3 + 2*5 = 13 and 1*4 + 2*6 = 16. Reversing the order, M*x, is an error because the 2×2 has two columns but x has one row.

Multiply 3×3 (and non-square) matrices

The rule is the same for any sizes. The MathWorks mtimes reference uses a 2×3 matrix times a 3×3 matrix, which gives a 2×3 result:

A = [1 3 5; 2 4 7];
B = [-5 8 11; 3 9 21; 4 0 8];
C = A*B
C =
    24    35   114
    30    52   162

Check: A(2,:)*B(:,3) is 162, which equals C(2,3). A 3×3 times a 3×3 is 3×3 by the same rule.

Dot and outer products from vectors

With A = [1 1 0 0] (1×4) and B = [1;2;3;4] (4×1), A*B is the scalar 3 (a dot product), while B*A is a 4-by-4 matrix (an outer product).

Matrix multiplication versus element-wise multiplication

MATLAB distinguishes array operations (.*, ./, .^), which act element by element, from matrix operations (*, /), which follow linear algebra. With the same A and B as in the 2×2 example, A*B is [6 15; 10 20] while A.*B is [3 0; 2 20]. * requires the inner dimensions to match; .* requires compatible sizes under implicit expansion. If either input is a scalar, A*B equals A.*B and the order does not matter; otherwise matrix multiplication is generally not commutative. Indexing the pieces you multiply is covered in the MATLAB indexing guide.

Multiplying N-D arrays with pagemtimes

* works on 2-D inputs only. To multiply many matrices at once, use Z = pagemtimes(X,Y), which multiplies page by page (introduced in R2020b). Dimensions beyond the first two must be compatible and are expanded implicitly: an X of size 10x8x1x3 and a Y of size 8x10x4x1 give a Z of size 10x10x4x3. The MathWorks example X = ones(3,3,2); A = eye(3); Y = cat(4,A,2*A,3*A,4*A); Z = pagemtimes(X,Y) returns a 3x3x2x4 array. To transpose inside the call use pagemtimes(X,transpX,Y,transpY), where both option arguments are required and each is 'none', 'transpose' or 'ctranspose'. You do not need a for loop over pages.

Which operation do you need?

Goal Use Size rule
Matrix product of 2-D arrays A*B A m-by-p, B p-by-n gives m-by-n
Same, as a function call mtimes(A,B) Identical to A*B
Element-by-element product A.*B Compatible sizes (implicit expansion)
Dot product of a row and a column [1 1 0 0]*[1;2;3;4] 1-by-n times n-by-1 gives a scalar
Outer product [1;2;3;4]*[1 1 0 0] n-by-1 times 1-by-n gives n-by-n
Page-wise product of N-D arrays pagemtimes(X,Y) First two dims as for *; the rest compatible

Output and how to read it

The result has the row count of the first matrix and the column count of the second. Entry C(i,j) is row i of A times column j of B. Use size(C) to confirm the shape before using the result downstream. Empty inputs are allowed: an m-by-0 matrix times a 0-by-n matrix returns an m-by-n matrix of zeros.

Troubleshooting

  • Size mismatch error. MATLAB raises an error when the columns of the first matrix do not equal the rows of the second. Print both sizes with size, then transpose (A') or reorder the operands if that matches your intent.
  • Wrong numbers, no error. A square pair works with both * and .* but gives different answers. Confirm which one you meant.
  • Integer or table inputs. If one input is an integer type, the other must be a scalar, and integer operands cannot be complex. For tables and timetables the other operand must also be a scalar.
  • 3-D array times a matrix. * does not accept it; use pagemtimes.

Free alternatives

Tool Matrix product Element-wise Source
GNU Octave x * y: columns of x must agree with rows of y x .* y Octave manual, arithmetic operators
Python with NumPy A @ B or numpy.matmul(A, B); scalars are not allowed, and stacks of matrices are broadcast A * B NumPy reference, numpy.matmul

NumPy matmul treats arrays with more than two dimensions as stacks of matrices in the last two axes, the counterpart of pagemtimes, and raises an error if the last dimension of the first argument does not match the second-to-last of the second. Plotting results is covered in scatter plots in MATLAB.

Frequently asked questions

How do I multiply a matrix by a constant in MATLAB?

Use * or .* with the scalar: if either input is a scalar, A*B equals A.*B and every element is scaled.

What is the difference between * and .* in MATLAB?

* is the linear-algebra matrix product; .* multiplies corresponding elements. For A = [1 3; 2 4] and B = [3 0; 1 5] they give [6 15; 10 20] and [3 0; 2 20].

How do I multiply a 3-D array by a 2-D matrix?

Use pagemtimes(X,Y) (R2020b or later); it multiplies page by page and expands the extra dimensions.

Do I need a for loop to multiply matrices?

No. A*B handles 2-D matrices and pagemtimes handles stacks.

Sources

Follow CASRAI

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

Ask CASRAI · free to try

Ask about Matrix Multiplication in MATLAB: * Operator, mtimes and pagemtimes

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 →