- Last updated

- Save as PDF

- Page ID
- 89346

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{#1}}} \)

\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\id}{\mathrm{id}}\)

\( \newcommand{\Span}{\mathrm{span}}\)

\( \newcommand{\kernel}{\mathrm{null}\,}\)

\( \newcommand{\range}{\mathrm{range}\,}\)

\( \newcommand{\RealPart}{\mathrm{Re}}\)

\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

\( \newcommand{\Argument}{\mathrm{Arg}}\)

\( \newcommand{\norm}[1]{\| #1 \|}\)

\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)

\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)

\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

\( \newcommand{\vectorC}[1]{\textbf{#1}}\)

\( \newcommand{\vectorD}[1]{\overrightarrow{#1}}\)

\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}\)

\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{#1}}} \)

\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

## 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 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]\]

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 and to each other.

IMAGE

3c) Subtract matrix from matrix .

IMAGE

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

IMAGE

\[a\]

IMAGE

## Problems

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

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