## Matrices

### Matrix conventions

Matrices are arrays of numbers, arranged in rows and columns. When reading you read the rows first,then the column second. 1xn are called Row vectors. mx1 are called column vectors.

To add matrices you first need 2 identically sized and shaped matrices, from here you add each corresponding number to the other matrix.

### Matrix Subtraction

To subtract matrices you first need two identically sized and shaped matrices, from here you subtract each corresponding number to the other matrix.

### Scalar Multiplication

Scalar Multiplication means the multiplication of a number with a matrix, Multiply each number of the matrix with the provided number.

### Matrix Multiplication

Multiplication of a matrix with another matrix, the matrices don’t have to be the same size, however, the columns in the first matrix have to be the same size as the rows in the second matrix. Multiply the first number in the first row of the first matrix with the first number of the first column of the second matrix, then multiply the second number in the first row of the first matrix with the second number of the first column of the second matrix lastly add them together, repeat this for every number in the rows and columns.

(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58

We match the 1st members (1 and 7), multiply them, likewise for the 2nd members (2 and 9) and the 3rd members (3 and 11), and finally sum them up.

(1, 2, 3) • (8, 10, 12) = 1×8 + 2×10 + 3×12 = 64

Then you do the exact same as above but rather than using the 1st row in the 1st matrix you will use the 2nd row on the 1st matrix.

The end product should have the same amount of rows as the 1st matrix and the same amount of columns as the 2nd matrix.

### Identity Matrix

The “Identity Matrix” is the matrix equivalent of the number “1”:

• It is “square” (has same number of rows as columns)
• It can be large or small (2×2, 100×100, … whatever)
• It has 1s on the diagonal and 0s everywhere else
• Its symbol is the capital letter I

### Representing matrices as arrays

```//Addition
for ( i=0; i&lt;n; i++ ){
for ( j=0; j&lt;m; j++ ){
c[i][j] = a[i][j] + b[i][j];
}
}
```
```//Subtraction
for ( i=0; i&lt;n; i++ ){
for ( j=0; j&lt;m; j++ ){
c[i][j] = a[i][j] - b[i][j];
}
}
```
```//Scalar Multiplication
for ( i=0; i&lt;n; i++ ){
for ( j=0; j&lt;m; j++ ){
c[i][j] = s * a[i][j];
}
}
```
```//Matrix Multiplication
for ( i=0; i&lt;n; i++ ){
for ( j=0; j&lt;o; j++ ) {
e[i][j] = 0; /* initialise sum as 0 */
for ( k=0; k&lt;m; k++ ){
e[i][j] = e[i][j] + a[i][k] * d[k][j];
}
}
}
```