# MATLAB Programming/Vector and Matrices/Operations on Matrices

## Basic Matrix OperationEdit

### Addition and subtractionEdit

We can add and subtract the matrices if __both of them are of same number of rows and columns__.

Examples as follows:

```
>> a = [7,9,4;6,2,5]
a =
7 9 4
6 2 5
>> b=[2,0,1;4,7,3]
b =
2 0 1
4 7 3
>> % Addition of a and b matrices
a+b
ans =
9 9 5
10 9 8
>> % Subtraction of a and b matrices
a-b
ans =
5 9 3
2 -5 2
```

### Matrix multiplicationEdit

For matrix multiplications, there are 2 ways to do multiplications.

(i) Matrix multiplications (using symbol ***** or **mtimes**)

Requirements is that the __number of columns in the first matrix__ must be ** same** as the

__number of rows in the second matrix.__

As examples shown on the right , matrix A have 3 X 2 and matrix B have 2 X3

Therefore , 2 X ** 3** <->

**X 2 , hence, it fulfils the requirements above.**

__3__Also, take note that the resulting matrix sizes is based on __number of rows of first matrix__ with __number of columns in second matrix__.

```
>> A=[4,2,4;8,3,1]
A =
4 2 4
8 3 1
>> B=[3,5;2,8;7,9]
B =
3 5
2 8
7 9
>> mtimes(A,B)
ans =
44 72
37 73
>> A*B
ans =
44 72
37 73
```

Following examples shows what if the incorrect matrix dimension matches.

As shown, the matrix C have 5X4 and matrix D have 3X2

5X ** 4** <->

**X2, therefore it cannot fulfill the conditions and unable to solve it.**

__3__```
>> %Demonstrations what if matrices dimensions are incorrect
>> C=randi(10,5,4)
C =
2 10 10 3
2 10 4 5
3 5 2 1
5 5 8 2
1 4 4 10
>> D=randi(10,3,2)
D =
10 3
6 4
1 9
>> mtimes(C,D)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first
matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.
```

(ii) Dot product (Note: Only can be used if both matrices are same sizes) Taking examples above, it doesn't solve the equations as both matrix A and B above are not same size

```
>> A.*B % want to show dot product unable to solve this multiplication issues
Matrix dimensions must agree.
```

## Creating Random Integer MatrixEdit

To generate random integer matrix, can type the following **randi(IMAX,M,N)**

Note: **IMAX** is the maximum integer (starting from 1) and **M*****N** matrix

Examples as followed:

```
>> randi(50,3,4)
ans =
9 14 42 48
36 3 35 2
2 5 16 22
```

## TransposeEdit

Using the matrices above, we can transpose the matrices. Transpose matrices usually just switch the row to column and columns into rows. The picture shown just demonstrate the transpose operation. One of the application of transpose the matrix is to crytography.

There are two ways to go about it. One is add ' to the end of the matrix that are going to be transpose or function **transpose**.

Back to the magic square example above, we are going to transpose it. We can see , it is transpose along the diagonal line looks like this : \

```
>> % transpose matrix c to d
>> d = c'
d =
17 23 4 10 11
24 5 6 12 18
1 7 13 19 25
8 14 20 21 2
15 16 22 3 9
```

## DeterminantEdit

The determinant of matrix is a special number that is defined only for **square matrices**.

A determinant is used to find the solution of a system of linear equations and determine the inverse of a matrix.

Determinant of 2X2 matrix :

Determinant of 3X3 matrix :

```
>> A=[6,1,1;4,-2,5;2,8,7]
A =
6 1 1
4 -2 5
2 8 7
>> det(A)
ans =
-306.0000
```

## InverseEdit

Inverse of matrix is reciprocal matrix where the formula is denoted by

, where the adj is representing adjoint of matrix.

**Note: Not all matrix have inverse, if their determinant is equal to zero.**

```
>>%matrix inversion using manual method
>> M=[2,-1,3;-5,3,1;-3,2,3]
M =
2 -1 3
-5 3 1
-3 2 3
>> %First we find the matrix determinant
>> DM = det(M)
DM =
-1.0000
>>%Since determinant IS NOT equal to 0, we can find the matrix inverses
>> AM = adjoint(M)
AM =
7.0000 9.0000 -10.0000
12.0000 15.0000 -17.0000
-1.0000 -1.0000 1.0000
>> (1/DM)*AM
ans =
-7.0000 -9.0000 10.0000
-12.0000 -15.0000 17.0000
1.0000 1.0000 -1.0000
%shortcut using function inv which should be same as manual calculation above
>> inv(M)
ans =
-7.0000 -9.0000 10.0000
-12.0000 -15.0000 17.0000
1.0000 1.0000 -1.0000
```

There are many applications of matrix such as:**Crytography** : where it is used to encrypt message codes. Matrices are used by programmers to code or encrypt letters. A message is made up of a series of binary numbers that are solved using coding theory for communication. As a result, the concept of matrices is used to solve such equations.

In physics, the Inverse matrix is used to explore electrical circuits, quantum mechanics, and optics. These matrices are crucial in the measuring of battery power outputs and the conversion of electrical energy into other useable energy by resistors. When applying Kirchhoff’s laws of voltage and current to solve problems, the inverse matrices are extremely significant.

## ReferencesEdit

^{[1]}^{[2]}^{[3]}^{[4]}

- ↑ https://web.archive.org/web/20220712153202/https://collegedunia.com/exams/applications-of-determinants-and-matrices-and-solved-examples-mathematics-articleid-2195
- ↑ https://web.archive.org/web/20220719154910/https://www.embibe.com/exams/inverse-matrix/
- ↑ https://web.archive.org/web/20220814062118/https://www.theclickreader.com/dot-products-and-matrix-multiplication/
- ↑ https://web.archive.org/web/20220814062138/https://www.theclickreader.com/matrix-transpose-determinants-and-inverse/