MATLAB Programming/Vector and Matrices/Operations on Matrices

Addition and subtraction
We can add and subtract the matrices if both of them are of same number of rows and columns.

Examples as follows:

Matrix multiplication
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  <->  3  X 2 , hence, it fulfils the requirements above.

Also, take note that the resulting matrix sizes is based on number of rows of first matrix with number of columns in second matrix. Following examples shows what if the incorrect matrix dimension matches.

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

5X  4  <->  3 X2, therefore it cannot fulfill the conditions and unable to solve it. (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

Creating Random Integer Matrix
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:

Transpose
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 cryptography.



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 : \

Determinant
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 : $$\mathbf{A} = \begin{bmatrix} a & b \\ c & d \\ \end{bmatrix} * |A| = ad - bc$$

Determinant of 3X3 matrix : $$\mathbf{A} = \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix} * |A| = a(ei-fh) - b(di-fg) + c(dh-eg) $$

Inverse
Inverse of matrix is reciprocal matrix where the formula is denoted by $$ {A}^{-1} = \frac{1}{|A|} adj A$$, where the adj is representing adjoint of matrix.

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

Real life applications
There are many applications of matrix such as:

Crytography : Matrix 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.

To perform this matrix operation, the message is first broken up into blocks of fixed size, and each block is represented as a vector of numbers. The public key includes a matrix, called the encryption matrix or the public matrix, that is used to transform each vector of numbers. The resulting transformed vectors are then raised to the exponent modulo the modulus to get the ciphertext.

To decrypt the message, the private key is used, which consists of two parts: the modulus and the inverse of the exponent modulo a certain value. The inverse of the exponent is used to raise the ciphertext to a power that will reverse the matrix transformation and recover the original message blocks. Image Processing : One of the matrix operations applicable in real life is image processing. We will look into one of the simpler examples such as image blurring.

For example, we want to apply blurring to an supposedly B&W image with an example of matrix shown here where each values in pixel represent a grayscale values from 1 to 255.

We will use matrix convolution to takes the average of the pixel values in a 3x3 neighborhood around each pixel in the image.

To apply this filter, we will slide the matrix over each pixel in the image and perform a matrix multiplication. For example, to blur the pixel at center in the image, you would take the 3x3 neighborhood around it, multiply each pixel value by the corresponding value in the blur filter matrix, and then add up the results. The final value is the new pixel value for the blurred image. We repeat this process for every pixel in the image, and the result is a new image that looks blurrier. Circuit analysis: In electrical engineering, the Kirchoff's Voltage Law (KVL) states that the sum of all voltages around a closed loop in a circuit must be equal to zero. This law can be used to write a set of linear equations for a circuit with m loops. These equations can be arranged in a matrix, known as the loop-branch incidence matrix.

Based on this examples shown:

Loop M1 :

Loop M2 :

[ R1        -R3              0  ]  [ I1 ]   =   [ V1 ]

[-R3        R2+R3     -R2 ]  [ I2 ]  =   [ -V2 ]

[ 0          -R2              1  ]  [ V2 ] =   [  0  ]

Using symbolic math tool box, we can solve the equations