Learn how to create some simple variables, including scalars, vectors, and matrices.
- [Narrator] Let's create some simple variables. We'll make a scalar, a vector, and a matrix. To start off we'll go to the command window and I'll clear our workspace with clear all. Looks good. Creating scalars is really simple. We create a variable name, myScalar and we'll just set it equal to ten. If we add a semicolon at the end of the line it will suppress the output in the command line here. So if I press enter now we don't see myScalar = 10 return from MATLAB but we can see that it's been created here in the workspace.
Creating a vector is pretty easy as well. We'll create a variable called myVector and to create a vector we use square brackets and if it's a row vector we separate the elements with a space. So I'll do four, five and six. I won't do a semicolon to suppress the output this time so we can see that myVector = 4 5 6 and it's also created over in the workspace. Let's create a column vector. I'll call it myVecorCol for column. And to create a column vector we still use square brackets but we separate the elements with semicolons.
So I'll do seven, eight, and nine. And we see that a column vector is created. I'm going to go ahead and clear the screen. And let's see how to create a matrix. We'll say myMatrix. And we build this one almost exactly the way we do with vectors. We'll create the first row in our matrix four, five, six, and then we separate that with a semicolon, seven, eight, nine, and then I'll put in one, two, three.
So we'll create a three by three matrix. Pressing enter we see that our matrix is created and how do we pull elements out of these matrices or vectors? That's pretty easy as well. We just index them. So we can say myMatrix and then we put in some parenthesis and this is going to take two arguments. The first is the row we want and the second is the column. So if we wanted to extract the number five from my matrix that would be row number one, column number two and MATLAB says "Yep, that's five." We can also get a whole row or column if we want.
So for instance, if we wanted to extract the middle row from myMatrix we can say myMatrix and use our parenthesis, and we want row two, and we want all columns of row two. And the way we tell MATLAB we want all columns is to use a colon. If we hit enter we see that we get back a row vector of seven, eight, and nine. I'm going to clear the screen real quick, bring myMatrix back up so we can see it. There we go. So let's go ahead and extract a column vector.
I'll say myMartix and I can tab complete that. Let's extract the last column, so we want all of the rows in column three. And there we go, six, nine, and three. We can also extract subset of the rows or columns if we want. So again I'll bring up myMatrix just so we can see it. And let's pull out the two and three of row three. So I can say myMatrix and now I want row three but I only want columns two and three.
So we say two, colon, three to give me columns two through three. If I hit enter, we see that we get two and three back. And let's finish up by clearing the screen.
Released
4/16/2018- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models
Share this video
Embed this video
Video: Create variables