"

4. Vector and Matrix Operations

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 roles 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]

What is a Vector?

Vectors are actually a subset of matrices that have either one row or one column. For example, we could take the first row of the 2×3 matrix from above and create a 1×3 vector.

\left[ \begin{matrix} 2 & 4 & 65 \end{matrix} \right]

Similarly, a column vector could be extracted from the 4×3 matrix shown above by taking the four values in the far right column.

\left[ \begin{matrix} -21\\ 25\\ 12\\-11 \end{matrix} \right]

Inputting Vectors

Creating vectors in MATLAB is very straightforward. To create a single row vector, begin with a a variable name of your choosing on the left, followed by an equals sign, and then place the values inside of hard brackets. Spaces or commas can be used to separate values, as shown in the two examples below, which produce identical vectors.

>> rowVector1a = [1 2 3]

>> rowVector1b = [1, 2, 3]

As with most things in MATLAB there are many ways to accomplish the same task. Most coders adopt their own preferences, such that scripts take on unique styles based on the programmer — similar to reading essays authored by different writers. Sometimes the decision comes down to the application or need.

For example, if you would like to create a vector with evenly spaced values, then semicolons are helpful. Semicolons can be used to create a row vector, as demonstrated below, where the value in the middle specifies the step size between the first and last values. This method works very well

>> rowVector1c = [1:1:3]

Yet another way to produce a vector is to use the built-in MATLAB command linspace. This command takes in three values — the first is the beginning value, the second is the ending value, and the third specifies the number of values. This is a helpful approach if you know the number of values you want in your vector. The code below will produce the same row vector as all the examples listed above.

>> rowVector1d = linspace(1,3,3)

Column vectors are just row vectors rotated 90 degrees. So one method for creating a column vector is to “transpose” a row vector. For example, I can take vector1a and use the transpose symbol which is a single quote in MATLAB to rotate the vector 90 degrees to get a column vector with 3 rows. Note the change in variable name.

>> colVector1a = [1 2 3]'

Column vectors can also be produced using semicolons to indicate where a new line should begin. That approach is shown below.

>> colVector1b = [1; 2; 3]

Inputting Matrices

It is also 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.

>> matrix1a = [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,

>> matrix1b =[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,

>> matrix3 =[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

Vector and 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. True matrix multiplication is a fundamental concept of linear algebra that does not involve element-by-element multiplication. For most coding applications in this course, we will want element-by-element multiplication. That is, we will want to multiply the value in the first row and first column with another matrix’s value stored in the first row and first column, and so on and so forth. That operation in MATLAB is relatively straightforward as it simply requires the programmer to add a period (.) in front of the multiplication symbol (*). The same is true for division (/). To carry out element-by-element multiplication or division, the two matrices must be identical in size, that is, have the same number of rows and columns.

Vector and Matrix Arithmetic in Matlab

Element-by-element vector addition is simple in MATLAB where again, the vectors in question must be the same size. You can create new vectors within the addition operation, as shown below.

>> vectorAdd1a = [1 2 3]+[4 5 6]

Alternately, you can use previously created variables as shown here.

>> vectorAdd1a = rowVector1a+rowVector1a

Multiplication and division follow similarly, with the subtle change of needing a period (.) ahead of the operator (* or /) to indicate that the operation should be done on an element-by-element basis. See the examples below.

>> vectorMultiply1a = [1 2 3].*[1 2 3]

>> vectorMultiply1b = rowVector1a.*rowVector1a

Note that without the period, the computations will not be done element-by-element, and will produce results that follow the rules of linear algebra. You can try these operations for yourself in MATLAB to observe the differences in results.

>> vectorMultiply1c = [1 2 3]*[1 2 3]

>> vectorMultiply1d = rowVector1a*rowVector1a

Matrix arithmetic follows the same rules listed above, such that careful attention should be paid to the use of periods in multiplication and division. Sometimes engineers need to make use of powers and in MATLAB the same rules about periods also apply, where a period ahead of the power (^) must be used to indicate element-by-element operations.

>> matrixSquared = matrix1a.^2

Finally, sometimes there is a need to concatenate vectors or matrices, which is to say take multiple vectors or matrices and group them into a single entity. An example of that operation is shown below. Note that this can only be done with the vectors or matrices being concatenated have matching dimensions — e.g. placing two matrices side-by-side requires a matching number of rows, whereas placing them on top of each other would require a matching number of columns. Try the examples shown below to observe the difference in results between the concatenated matrices created with and without the semicolon.

>> matrixA = [1 2 3; 4 5 6]

>> matrixB = [3 2 1; 6 5 4]

>> matrixConcatenated1 = [matrixA; matrixB]

>> matrixConcatenated2 = [matrixA matrixB]

Note that this can only be done with the vectors or matrices being concatenated have matching dimensions — e.g. placing two matrices side-by-side requires a matching number of rows, whereas placing them on top of each other would require a matching number of columns.