Chapter 3: MATRIX Operations

Introduction

MATLAB serves as a powerful tool to solve matrices. To use matrices as a tool to solve equations or represent data a fundamental understanding of what a matrix is and how to compute arithmetical operations with it is critical.

What is a Matrix?

A matrix is a rectangular array or grid of values which arranged in rows and columns. Matrices are used to operate on a set of numbers with variations of traditional mathematical operations. Matrices serve valuable rolls within many engineering and mathematic tasks due to their useful ability to effectively store and organize information. Understanding matrices proves valuable when trying to solve systems of equations, organizing data collected during experiments, computing mathematical operations on large quantities of numbers, and complicated applications in linear algebra, machine learning, and optimization.

When describing matrices, we will name them based on the number of rows and columns. For example, the following matrix is a 2×3 matrix as it has two rows and three columns.

$\left[\begin{matrix}2&4&65\\3&2&-8.5\\\end{matrix}\right]$

And this matrix is a 4×3 matrix:

$\left[\begin{matrix}\begin{matrix}1&-21\\2&25\\\end{matrix}\\\begin{matrix}3&12\\4&-11\\\end{matrix}\\\end{matrix}\right]$

Matrix Arithmetic

Matrices are an effective way to modify an entire set of numbers in one operation. Simple ways to modify matrices include addition, subtraction, multiplication, and division by a scalar, or individual number. When completing these operations, complete the calculation with each number in the matrix, as denoted below.

$\left[\begin{matrix}1&2\\4&3\\\end{matrix}\right]+2=\left[\begin{matrix}1+2&2+2\\4+2&3+2\\\end{matrix}\right]=\left[\begin{matrix}3&4\\6&5\\\end{matrix}\right]\gets Answer$

$\left[\begin{matrix}2&-4\\1.5&3\\\end{matrix}\right]\ast3=\left[\begin{matrix}2\ast3&-4\ast3\\1.5\ast3&3\ast3\\\end{matrix}\right]=\left[\begin{matrix}6&-12\\4.5&9\\\end{matrix}\right]\gets Answer$

Matrices with the same dimensions (i.e. two 2×2 matrices) can have more mathematical operations completed with them. For example, you can add or subtract matrices with the same dimensions by completing operations on the values in each corresponding location in a matrix. The following shows a template for adding or subtracting two matrices.

$\left[\begin{matrix}a&b\\c&d\\\end{matrix}\right]+\left[\begin{matrix}e&f\\g&h\\\end{matrix}\right]=\left[\begin{matrix}(a+e)&(b+f)\\(c+g)&(d+h)\\\end{matrix}\right]$

Multiplying matrices is more difficult than adding and subtracting and does not follow the format listed above. The process known as element-wise matrix multiplication is shown below. This process for multiplying matrices is a fundamental concept of linear algebra and occurs when working with matrices in MATLAB.  Be aware of the general form shown below and that it can be extrapolated to include matrices of different sizes. An alternative method of multiplying two matrices that are the same size is called component-wise multiplication, which would follow the same form as the matrix addition shown above. The procedure for coding these into MATLAB are shown below.

Vectors and Matrices in MATLAB

Inputting Matrices

It is easy to input matrices into MATLAB scripts. To make a standard matrix in the command window, use the following format with values of a matrix listed with spaces between each value. Use a semicolon to separate each line of the matrix. To see how this process looks within MATLAB, refer to the examples at the end of this section.

`>> [1 2 3;4 5 6;7 8 9]`

Which produces $\left[\begin{matrix}1&2&3\\4&5&6\\7&8&9\\\end{matrix}\right]$ in MATLAB.

Note that to create an array list each number in a row separated only by spaces. To move down to a new row, use a semicolon. To save time making a large array, a colon can be used to “list” numbers. For example, 1:5 would create a row containing 1, 2, 3, 4, and 5. For example,

`>> [1:3;4:6;7:9]`

creates the same matrix as the first example. If you would like to create a matrix that counts by a unit other than one, add a second colon that denotes what numbers will be included. For example,

`>> [2:2:10;12:2:20]`

will create the following 2 row by 5 column matrix which counts by twos between 2 and 10 in the top row and 12 and 20 in the bottom row

Matrix Operations and Concatenating Matrices

Examples

1) Enter the following matrix efficiently into MATLAB.

$\left[\begin{matrix}1&2&3\\7&8&9\\\end{matrix}\right]$

2) Enter the following matrix efficiently into MATLAB.

$\left[\begin{matrix}1&2&3&4&5&6\\7&9&11&13&15&17\\18&18.5&19&19.5&20&20.5\\\end{matrix}\right]$

3) Use the following matrices in the following parts.

$a=\left[\begin{matrix}1&2\\3&4\\\end{matrix}\right]$ and $b=\left[\begin{matrix}2&4\\6&8\\\end{matrix}\right]$

3a) Input the above matrices into MATLAB. Assign each the variable name shown.

Note that by placing semicolons at the end of the line the output is suppressed. As a result, the actual matrices are not printed in the code, which saves space in this instance.

IMAGE

3b) Add matrix $a$ and $b$ to each other.

IMAGE

3c) Subtract matrix $a$ from matrix $b$.

IMAGE

3d) Multiply matrix $a$ and matrix $b$ using component-wise multiplication.

IMAGE

3e) Multiply matrix $a$ and matrix $b$ using matrix multiplication.

IMAGE

Problems

Efficiently type the following matrices into MATLAB’s command window.

1. $\left[\begin{matrix}1&4&7\\2&3&4\\-3&0&3\\\end{matrix}\right]$

1. $\left[\begin{matrix}1&2&3&4&5&6\\10&11&12&13&14&15\\\end{matrix}\right]$

1. $\left[\begin{matrix}1&4&7&10&13&16\\-2&0&2&4&6&8\\\end{matrix}\right]$

1. $\left[\begin{matrix}0&0.5&1&1.5&2\\1&1.25&1.5&1.75&2\\5&4&3&2&1\\\end{matrix}\right]$

Use these matrices to complete the following computations using MATLAB.

$a=\left[\begin{matrix}-8&4\\5&12\\\end{matrix}\right];\ \ b=\left[\begin{matrix}3&5\\2&3\\\end{matrix}\right];\ \ c=\left[\begin{matrix}-2&1.5\\12&-4.25\\\end{matrix}\right];\ \ d=\left[\begin{matrix}-2&0\\2&4\\\end{matrix}\right]$

1. $a+b$

1. $\left(a+b\right)\ast c$

1. $\left(c\ast d\right)$

1. $\left(a-b\right)\ast(c+d)$