All courses › Intermediate Programming › Matrix operations

Matrix operations

A * B between two NumPy arrays is elementwise multiplication, while np.dot(A, B) or A @ B is true matrix multiplication. reshape changes an array's shape without changing the data, and -1 lets NumPy work out the last dimension itself.

A @ BA \,@\, Bmatrix multiplication
A∗BA * Belementwise multiplication (requires the same shape)

Symbols

A, BA,\ Bmatrices (2D NumPy arrays)

Example

np.arange(12).reshape(4, -1).shape gives (4, 3), since 12/4=312/4 = 3.

A * B and A @ B give very different results — use @ when you mean matrix multiplication.
Practise numerics with NumPy for free →

← linspace and arange · try / except →

Part of Intermediate Programming: Numerics with NumPy.