Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 45 ยท Operators and Math

Arithmetic: Matrix vs Element-wise

Learn when MATLAB and Octave operators work on whole matrices or element by element: * vs .*, ^ vs .^, transpose, broadcasting and precedence.

In MATLAB and Octave every value is a matrix, so the arithmetic operators have to answer a question that most languages never face: when you write A * B, do you mean the linear-algebra matrix product, or do you want to multiply matching elements? The language answers with two families of operators. The plain ones (*, /, ^) follow the rules of matrix algebra. The ones with a dot in front (.*, ./, .^) work element by element.

Mixing these up is the most common beginner bug in the language, and it is made worse by the fact that for square matrices both versions run without an error and quietly give different numbers. This lesson shows the difference, the error messages you will meet, how arrays of different sizes combine (implicit expansion) and how operator precedence works.

MATLAB and Octave are not installed on this site, so matlab blocks are read-only and the TryIt blocks are Python (NumPy) stand-ins. In NumPy the roles are reversed: * is element-wise and @ is the matrix product. The comments in each stand-in name the MATLAB operator being imitated.

Two kinds of operators

Matrix
  • / ^ follow linear algebra.
Element-wise .* ./ .^ act on matching elements.
Plus and minus Always element-wise; no dot needed.

When in doubt, add the dot.

1 / 2

Matrix product versus element-wise product

For two matrices, A * B is the matrix product: each entry is a row of A multiplied with a column of B and summed. It requires the number of columns in A to equal the number of rows in B. A .* B multiplies matching elements and requires the two arrays to be the same size (or compatible, see below).

MATLAB
A = [1 2; 3 4];
B = [5 6; 7 8];

A * B        % 19 22 ; 43 50
A .* B       %  5 12 ; 21 32
A .^ 2       %  1  4 ;  9 16     every element squared
A ^ 2        %  7 10 ; 15 22     A * A, the matrix squared
2 .^ A       %  2  4 ;  8 16     2 raised to each element
A ./ B       % 0.2000 0.3333 ; 0.4286 0.5000

The same split applies to division. A ./ B divides matching elements. A / B means A * inv(B) and A \ B means inv(A) * B; you will use the backslash form in Linear Algebra Essentials. Addition and subtraction have no matrix/element-wise distinction: A + B and A - B are always element-wise. Multiplying or dividing by a scalar is also always fine without a dot: 2 * A and A / 2.

Python Matrix vs element-wise

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last lines show why shape matters. A column times a row (u * v) is a 3-by-3 outer product; a row times a column (v * u) collapses to one number, the inner (dot) product. A column times a column is not a valid matrix product at all.

The beginner bug and its error messages

Suppose you want y = x^2 for every x in a vector. You write the natural thing:

MATLAB
x = 0:0.5:2;
y = x^2
% Error using ^
% Incorrect dimensions for raising a matrix to a power. Check that the matrix is
% square and the power is a scalar. To perform elementwise matrix powers, use '.^'.

Likewise x * x for a row x fails with "Incorrect dimensions for matrix multiplication ... To perform elementwise multiplication, use '.*'." These errors are friendly: the fix is named in the message. The nasty case is a square matrix, where A * B and A ^ 2 are legal and simply mean something else than you intended. There is no error, only wrong numbers, so build the habit early: when you mean "for every element", type the dot.

A practical rule: use *, / and ^ only when you are consciously doing linear algebra with matrices or vectors, and use .*, ./ and .^ for everything that is an arithmetic formula applied to data, such as y = a .* x .^ 2 + b .* x + c.

Transpose: ' versus .'

The apostrophe ' is the conjugate transpose. For real numbers it just swaps rows and columns. For complex numbers it also flips the sign of the imaginary part. The dot-apostrophe .' is the plain transpose that never conjugates.

MATLAB
Z = [1+2i, 3-1i];
Z'     % [1-2i; 3+1i]   conjugated
Z.'    % [1+2i; 3-1i]   not conjugated

If you only work with real data the two are identical, but many people use .' out of habit so that code stays correct when complex numbers appear.

Implicit expansion

When two arrays have different sizes, MATLAB (R2016b and later) and Octave compare their sizes dimension by dimension. Two sizes are compatible when they are equal or one of them is 1. The array with size 1 is conceptually repeated to match; no copies are really made. This is called implicit expansion (NumPy calls it broadcasting).

MATLAB
M = [10 20 30; 40 50 60];
M - mean(M)           % mean(M) is 1x3: subtracted from every row
M ./ sum(M, 2)        % sum(M, 2) is 2x1: each row divided by its own total

r = [1 2 3];
c = [10; 20; 30];
c + r                 % 3x3 table: 11 12 13 ; 21 22 23 ; 31 32 33

Before expansion existed you had to write bsxfun(@minus, M, mean(M)) or use repmat. You will still see bsxfun in older code. Sizes that do not fit raise "Arrays have incompatible sizes for this operation": for example a 2-by-3 matrix plus a 1-by-2 row.

Python Transpose, expansion and precedence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the stand-in, keepdims=True keeps the row sums as a 2-by-1 column so the shapes expand correctly; MATLAB's sum(M, 2) already returns a column. The last line is written with explicit parentheses because Python reads 2 ** 3 ** 2 right to left (giving 512), while MATLAB evaluates repeated powers left to right.

Operator precedence

From highest to lowest, MATLAB evaluates:

  1. Transpose and power: ', .', ^, .^
  2. Unary plus, unary minus and logical not: +x, -x, ~x
  3. Multiplication and division: *, .*, /, ./, \
  4. Addition and subtraction: +, -
  5. The colon operator :
  6. Comparisons: <, <=, >, >=, ==, ~=
  7. Element-wise and: &, then element-wise or: |
  8. Short-circuit and &&, then short-circuit or ||

Three consequences surprise newcomers. First, -2^2 is -4, because power binds tighter than the unary minus; write (-2)^2 for 4. Second, 2^-1 is 0.5: a unary minus directly after the power operator is allowed and binds to its exponent. Third, 1:n+1 means 1:(n+1) because addition ranks above the colon. When an expression is longer than a line, use parentheses; they cost nothing and remove doubt.

Common patterns

  • Normalize columns: X ./ vecnorm(X) or (X - mean(X)) ./ std(X).
  • Multiplication table: (1:9)' * (1:9) is a matrix product of a column and a row.
  • Polynomial on data: y = 3 .* x .^ 2 - 2 .* x + 1.
  • Dot product: sum(a .* b) or a * b' for two row vectors, or the built-in dot(a, b).
  • Division by zero is not an error: 1/0 is Inf and 0/0 is NaN.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. For A = [1 2; 3 4], what is A .^ 2?

    The dotted operator squares each element. A ^ 2 (no dot) would be the matrix product A * A, which gives [7 10; 15 22].
  2. 2. A row vector x is 1-by-5. Which line raises an error about matrix dimensions?

    x * x asks for a matrix product of a 1-by-5 with a 1-by-5, whose inner dimensions (5 and 1) do not agree. Use x .* x for squares of the elements or x * x' for the dot product.
  3. 3. What is the size of c + r when c is 3-by-1 and r is 1-by-4?

    Implicit expansion repeats the column across 4 columns and the row down 3 rows, giving a 3-by-4 table.
  4. 4. What does -2^2 evaluate to?

    The power operator has higher precedence than unary minus, so the expression means -(2^2) = -4.
  5. 5. How do ' and .' differ for a complex row vector?

    The apostrophe is the conjugate transpose, so it also flips the sign of the imaginary parts. .' is the plain transpose.

Summary

  • *, / and ^ follow matrix algebra; .*, ./ and .^ act element by element; + and - are always element-wise.
  • For square matrices the wrong operator gives wrong numbers silently, so use the dotted forms for formulas applied to data.
  • ' is the conjugate transpose and .' the plain transpose; they differ only for complex data.
  • Implicit expansion lets a size-1 dimension stretch to match the other array; incompatible sizes give an error.
  • Power binds tighter than unary minus, repeated powers go left to right, and parentheses are always welcome.

Next: put these operators to work in Linear Algebra Essentials.