Facebook Pixel
Mathos AI logo

Matrix Operations

Matrix Operations

A matrix is a rectangular array of numbers arranged in rows and columns. Just like ordinary numbers, matrices can be added, subtracted, and multiplied, but they follow a specific set of rules.

Addition and Subtraction

To add or subtract two matrices, they must have the exact same dimensions (same number of rows and columns). You simply add or subtract the corresponding elements in each position.

Let A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} and B=[5678]B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}.

A+B=[1+52+63+74+8]=[681012]A + B = \begin{bmatrix} 1+5 & 2+6 \\ 3+7 & 4+8 \end{bmatrix} = \begin{bmatrix} 6 & 8 \\ 10 & 12 \end{bmatrix}

AB=[15263748]=[4444]A - B = \begin{bmatrix} 1-5 & 2-6 \\ 3-7 & 4-8 \end{bmatrix} = \begin{bmatrix} -4 & -4 \\ -4 & -4 \end{bmatrix}

Scalar Multiplication

Scalar multiplication involves multiplying a matrix by a single real number (called a scalar). To do this, multiply every single element inside the matrix by that scalar.

For example, to find 3A3A:

3A=3[1234]=[3(1)3(2)3(3)3(4)]=[36912]3A = 3 \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} = \begin{bmatrix} 3(1) & 3(2) \\ 3(3) & 3(4) \end{bmatrix} = \begin{bmatrix} 3 & 6 \\ 9 & 12 \end{bmatrix}

Example: Combining Operations

Let's compute 3A2B3A - 2B using the matrices above. First, find 2B2B: 2B=2[5678]=[10121416]2B = 2 \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} = \begin{bmatrix} 10 & 12 \\ 14 & 16 \end{bmatrix}

Now, subtract 2B2B from 3A3A: 3A2B=[36912][10121416]=[7654]3A - 2B = \begin{bmatrix} 3 & 6 \\ 9 & 12 \end{bmatrix} - \begin{bmatrix} 10 & 12 \\ 14 & 16 \end{bmatrix} = \begin{bmatrix} -7 & -6 \\ -5 & -4 \end{bmatrix}

Matrix Multiplication

Multiplying two matrices is more complex. To multiply matrix AA by matrix BB (to get ABAB), the number of columns in AA must equal the number of rows in BB. If AA is an m×nm \times n matrix and BB is an n×pn \times p matrix, the resulting matrix will have dimensions m×pm \times p.

To find an element in the resulting matrix, you compute the dot product of a row from the first matrix and a column from the second matrix.

Let's calculate ABAB: A=[1234],B=[5678]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}

AB=[(1)(5)+(2)(7)(1)(6)+(2)(8)(3)(5)+(4)(7)(3)(6)+(4)(8)]AB = \begin{bmatrix} (1)(5) + (2)(7) & (1)(6) + (2)(8) \\ (3)(5) + (4)(7) & (3)(6) + (4)(8) \end{bmatrix}

AB=[5+146+1615+2818+32]=[19224350]AB = \begin{bmatrix} 5 + 14 & 6 + 16 \\ 15 + 28 & 18 + 32 \end{bmatrix} = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}

Now, let's calculate BABA:

BA=[5678][1234]BA = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}

BA=[(5)(1)+(6)(3)(5)(2)+(6)(4)(7)(1)+(8)(3)(7)(2)+(8)(4)]BA = \begin{bmatrix} (5)(1) + (6)(3) & (5)(2) + (6)(4) \\ (7)(1) + (8)(3) & (7)(2) + (8)(4) \end{bmatrix}

BA=[5+1810+247+2414+32]=[23343146]BA = \begin{bmatrix} 5 + 18 & 10 + 24 \\ 7 + 24 & 14 + 32 \end{bmatrix} = \begin{bmatrix} 23 & 34 \\ 31 & 46 \end{bmatrix}

Important Note: Notice that ABBAAB \neq BA. Unlike regular numbers where 2×3=3×22 \times 3 = 3 \times 2, matrix multiplication is not commutative.