Variables for numbers, and indexing in MATLAB

Mike X Cohen
A free video tutorial from Mike X Cohen
Neuroscientist, writer, professor
4.6 instructor rating • 20 courses • 106,403 students

Learn more from the full course

MATLAB onramp 2020: coding, concepts, confidence, and style

Learn the key MATLAB programming skills that separate experts from novices: debugging, functions, and visualization

10:19:56 of on-demand video • Updated February 2021

  • Introduction to MATLAB
  • How to write scripts and functions
  • Visualize data in 2D and 3D
  • Control statements
  • Develop your programming style
  • Think like a programmer
  • Find and fix bugs!
  • Learn the most common coding mistakes and how to avoid them
  • Strategies for planning and organizing your code
English [Auto] Now that you know what variables are it's time to start learning specific categories or types of variables. So in this video we are going to begin with numbers so numeric variables these are variables that store numbers in particular things called scalar is vectors and matrices. I'm gonna introduce you to these concepts here in this video. Now as a reminder almost every video not every single video but most videos in this course have a script. They have a matlab dot m file that goes along with that video. And so you can access them all by downloading the zip file in the first lecture of each section of this course. And then it's the name of the file is always going to be on ramp. That's the name of the course variables that corresponds to the name of the section. And then some you know single word or sometimes a couple of words that is referencing this particular video. So this video is about variables but variables for numbers. And so the file is called numbers and I've shown you previously how to open up files but just as a quick reminder you can either click on home or you can click on Ed.. Both will get you to the same place and then you click on open and then you have to search through and find that particular file. In this case it's numbers. And then you want to open it. All right. So what I'm going to show you now is how to create variables that correspond to numbers. In fact the first one is called a scalar which I don't even have here. So just as a reminder of how this is spelled so scalar and a scalar is just a single number. So I'm gonna write S equals and let's say I don't know four point three. So now this is a variable s and it corresponds to a scalar four point three. And of course we have the semicolon here to suppress the output so that not every single line of code gets repeated like this in the command window. OK. So here we are going to create vectors. A vector is an ordered list of numbers. Now in MATLAB you can carbonate numbers into a vector using square brackets. In general square brackets are used for concatenation. So this right here is incorrect and in fact Matlab is giving us an error message here. It is not correct to write one for three. Maybe you think you know this might be how you would write it on a piece of paper but we need to tell Matlab that these should be these three individual numbers should be concatenated they should be grouped together and that is done using the square brackets like this. So just to show you here we get a vector. So the answer is 1 4 3. So now I'm going to assign this vector to the variable which is also called vec. Now when you create a vector in this way like this with numbers and spaces between them and square brackets concatenate them then this is always going to turn out to be a row vector vectors can be oriented as a row like this and they're flat and they go across or they can be oriented as a column which would mean 1 and then 4 below that and then the three below that. So to transform a row vector into a column vector you can use an operation called the transpose operation. And in MATLAB you can implement the transpose operation using a single quote. This is not a double quote. Like if someone is talking and you're writing about it this is just a single quotation. Now that transforms a row vector into a column vector so you can see when I copy and paste this into the command we still get one for three of course. But before it was one for three across. That's a row vector. Now it's one for three down. That is a column vector. So that's one way to transform a row vector or to create a column vector. The second way is to put semicolons after each of the individual numbers. Now this can be a little bit confusing at first because there's some overloading. This is a concept in coding called overloading. I'll write that down over loading overloading the term overloading means when the same character or the same operation or the same function has different meanings depending on the context. So here we are using a semicolon at the end of the line to suppress the output. So we're not getting a bunch of output printing out in the command window but here when the semicolon happened or appears inside these square brackets we are actually using that to indicate vertical concatenation. So it's slightly different use of the same character. So let's see what this code. Now look like so instead of having spaces we have a semicolon in between the numbers and you can see that that does vertical concatenation. So this is like the end of the first row and then this is the second row and here's the end of the second row. And then here's the third row. Okay. So here you go two ways to create a column vector. Now several videos ago I showed you how to use the colon operator to create numerical sequences. And here I'm showing you that you can take the numerical sequence for example. This one is gonna count odd numbers going from 1 through 9. And you can use that to create a variable. So now I create a variable skip fact it's a vector and that vector will be these the sequence 1 through 9 in odd numbers. So we went from scales to vectors and then the next step up is to go from vector to a matrix so a scalar is a single number. A vector is a list of numbers and a matrix is like a spreadsheet of numbers. So it has rows and it has columns. Now notice that I'm mixing the two ways of separating numbers that I have already shown here. So here I showed separating numbers by spaces and here I showed separating numbers by semicolons. This is for a horizontal concatenation. This is for vertical concatenation. And here I'm actually mixing both of these. So this is pretty interesting. We have the first row which is gonna be one two three and then base a row breaks. Then we get a new row in the matrix and then seven eight nine and another row and then three five two. So now we can see this variable called a mat for a matrix is a three by three matrix. So three rows and three columns. Okay. So I can already tell that something is going wrong here. Otherwise I wouldn't have written this comment. And the problem here is that we have the first row with three elements. Second row with three elements. And this third row only has two elements in a matrix. Every row needs to have the same number of elements. You could also say the same thing about columns. So what happens here is that the first and the second row each contain three numbers three elements but the third row has only two elements. So Matlab is going to give us some kind of a an error message. Now later on throughout the course you will learn more about how to decode how to interpret and understand these Matlab error messages. But anytime you see an error message that says something about dimensions and not consistent the first thing you should think about is sizes looking at the sizes of the variables. So in this case we have the sizes and the sizes of the two rows are the same and then the size of the third row is different. Okay. So let's look at the variables that we have created so far. We can look at the variables that we've created in the MATLAB workspace by typing who's and then we get a list of all of the variables are ordered alphabetically with capitals first. So if we were to write let's say capital maybe I'll do capital z equals and just three for no good reason. So you can see the capital letters come first and then the lowercase letters. So then we can see the sizes. This is typically the first thing I look at when I type who's and I want to see what are the variables in the workspace. I usually look at the names and then the sizes and then the class tends to be the next most relevant thing. Now if you like to have that little widget you might also have the whereas it is the workspace module so you might also have this open and then you can see what's in your workspace immediately without typing who. I guess I'm a bit old school in this way as not so much and I'm old school I think I like to have a clean Matlab desktop environment and this is just adding visual noise that's what I think. So in this video I showed you how to create variables that contain numeric information so variables that contain numbers. Now so far all of the variables that we've created were fairly small and so really easy to work with. But what happens when these factors and matrices get larger and larger and you only want to access some small piece you know one or a few elements in the vectors or matrices for that you have to use something called indexing and that is the topic of the next video.