In this video, explore vectors and matrices, which provide the data that linear algebra routines use in their calculations.
- [Instructor] MATLAB is terrific at basic math, but its real power comes when you need to perform linear algebra. Linear algebra is a deep and complex topic but the basic elements are reasonably straight forward. In this movie I will introduce vectors and matrices, which provide the data that linear algebra routines use in their calculations. So let's start with some definitions. This is a matrix. Specifically it is a two row by three column matrix also called a 2 X 3.
So as you can see when you are describing the dimensions of a matrix or the size of a matrix you give the rows first and the columns second. You can also work with vectors. You have a row vector which is, as the name implies, a single row. It can be as long as you want. And there's also a column vector, which is the transpose of a row vector. Instead of a single horizontal row you have a single vertical column. With that quick introduction in mind let's switch over to MATLAB and create some vectors and matrices.
I'm working now in MATLAB and I have a blank command window ready to go. Let's say that I want to define a row vector, and again that's where you have just the values in a single row as opposed to a column. I'll name my variable rvec, so R-V-E-C, just shorthand for row vector and I will define it as the enumerated list of one, two, three, four, and five. So I typed a left square bracket followed by the numbers, one, two, three, four, and five all separated with a space.
Then I'll press the right square bracket and Enter and you see that I have my row vector as described. If I want to type a column vector I can do that by putting a semicolon between each of the values. So I'll call my column vector cvec, C-V-E-C, equal left square bracket and then I'll type 6; 7; 8; 9; then 10 right square bracket to close out and Enter and you see that I get a vertical list.
Another way to create a column vector is to provide the transpose of a row vector. I'll get into transpose in more detail later in the course, but just know that for vectors it will change a row vector to a column vector and a column vector to a row vector. So if I were to create a second column vector, so C-V-E-C two equals then I would type R-V-E-C, which is the variable name for my row vector, followed by an apostrophe or a single quote.
Then when I press Enter you see that I now have a column vector with the values one, two, three, four, and five which were in my row vector now they're just rearranged. You can also define vectors by stating ranges of values. So if I were to type R-V-E-C two for my second row vector, equal I could define it as the range of values from 11 to 15. So I'll type a square bracket and then 11:15 right square bracket, everything looks good, Enter and there are the values.
I discuss ranges and step values and other elements elsewhere in the course. So that's what we have for vectors now let's talk about matrices. A matrix is an array of values, and yes a vector is a special form of matrix. So a row vector is simply a one row matrix. So let's start by defining one the long way where we enumerate the values. So I'll type M-A-T one as my variable name then equal then a left square bracket and 14, 15, 16.
That'll be my first row of values then a semicolon to indicate a new row and then 17, 18, 19 then a right square bracket. So I have a 2 X 3 matrix it looks like, I press Enter, and yes there are my values. As with vectors you can define matrices using ranges. So let's say that I want to create a variable called M-A-T two, so I'll type that in, equal left square bracket and we'll make the first row the values from say 31 to 33.
So I'll type 31 then a colon then 33 to indicate the range followed by a semicolon to show I want to go to the next row and then 34:36. So I have 31 through 33, 34 through 36. The same number of value in each row, press Enter and there I have it. Now it might see like it's easy to keep track of the dimensions of different matrices and if you're only working with one or two or if everything is absolutely consistent then you're right it is fairly easy to remember, however, once you get into MATLAB and you start creating sequences of calculations it's difficult to know what to remember.
The dimensions of a matrix that you just produced and I'll show you why that happens elsewhere in the course, because things can change dimensions based on results, but just to show you now, and I will reiterate this later. If you want to discover the size of a matrix, the dimensions that is, the number of rows and columns, you can use the function size followed by a left parenthesis and then I'll do M-A-T two which is my second matrix, it should be a 2 X 3, press Enter and it is. The number of rows is two and the number of columns is three.
With that information by way of introduction to vectors and matrices we can start doing some math with them.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions