MATLAB Tutorial
Hey guys, this is Mt kids11, and today I'm going to be teaching you how to use a program called MatLab.
Now, MatLab is for usually, um, data mining and, uh, just dealing with data or matrices in general. So it's really helpful if you're a researcher, or a scientist, or an economist—pretty much anything. Maybe you're just taking linear algebra in school, and you want to deal with matrices and with large amounts of data, or even small amounts of data. MatLab is what you're going to probably be working with, and usually, your university will give it to you or something like that. It's a pretty nice piece of software.
So when you first open MatLab, you'll be presented with this window. It'll include a command window, um, of course, it'll include various subboxes around, which I'll be explaining a little bit in this tutorial. But primarily, what you're going to be focused on is the command window.
The command window essentially is a line-by-line command line where you can enter a piece of MatLab code, and it will evaluate it and tell you the result. Then you can type another command. So for instance, a valid MatLab command is just a mathematical expression. So for instance, I could do 1 + 2, and it would tell me the answer is 3. I could also do 3 + 4, and it would tell me the answer is 7. I could do all sorts of operations, you know, asterisk is times, slashes is division. Uh, you can use parentheses—all that stuff.
So right away, you can see MatLab has the functionality of doing arithmetic built right in. In addition, you can assign variables. So for instance, let's say I want to make a variable A, and I want to give it the value of 2. Um, and I just have to do that. I just say A = 2. What this allows me to do is I can say something like A - 1, and it'll tell me it's 1, because 2 - 1 is 1. I can do A * A, and it'll give me 4 in this case, or A * A * A, and it'll give me 8.
So already, I have a variable assigned to the value of 2, and I can just use that variable in place of 2 in any expression, and it'll basically be substituted in. Essentially, typing A is just like typing the number 2 in a MatLab expression. Now that I've assigned the variable A, as you can see in our workspace pane over here, it shows me all of the actively assigned variables.
So here's our variable A, and it says it has a value of 2. Here's also another variable called ANS, and ANS just has the value of the last expression you evaluated. So for instance, when I said here A * A * A, it said ANS equals 8, and that's because A * A * A is 2 to the power of 3, which is 8, and it just assigns the result to a variable. ANS is just for convenience. So now I can do ANS * 2, and then if I do ANS * 2 again, and ANS * 2 again, I'm just multiplying the result of the last expression by 2 again and again and again. But that's just a little aside.
So this is how to assign variables. In addition to this, you can assign variables to be a vector or a matrix, because if you couldn't do this, MatLab would be kind of pointless, because it's mainly made for working with matrices. So let's go ahead and make a variable B, and I'm going to show you how to assign it to a row vector, which is just a matrix with one row, and each column is a different component of the vector. So for instance, for a vector with the components 1, 2, 3, um, all you would do is have B equals, then a left bracket, then the components separated by spaces, then a right bracket.
And if I hit enter here, you can see it shows the vector just with spaces separating each column. All right, and this is really just a matrix with one row. Um, you can also create a column vector, which just has each component in a new row, and it has one column. And to do that, let's say C equals 1 semicolon 2 semicolon 3 close bracket. And if you do this, the semicolon basically denotes a split between each row. So here's our row vector, and you can see that, uh, each value is on a separate row.
Now you can combine these two sets of syntax to create a full-blown matrix. So I'll call this matrix D, and what you're going to do is have the same brackets around the values. Each row is going to be separated by spaces, and then each row will be separated by semicolons. So I could have 1 space 2 space 3—that'll be the first row. Semicolon, then 4 5 6, semicolon, 7 8 9 close bracket.
And now if I go ahead and evaluate this, my matrix will come out to have the rows that we thought it would. So all you have to do is separate each row by a semicolon, and that'll give you a matrix. So now you can also do normal operations that you would normally do to a variable on a matrix variable.
So for instance, I can multiply D by itself, just D * D, and it'll do matrix multiplication. So, you know, it'll take the dot product of the row vectors and the column vectors. If you don't really know how matrix multiplication works, you should probably learn that. But this will do that. So if I hit enter here, is the result of doing the matrix multiplication.
Now, I haven't really seen this used a lot, but you can also multiply every cell in one matrix by every cell in the next matrix. So let's say we already have our matrix D, and let's make a matrix E, and all the—it's going to be a 3x3 still—but let's make everything two. All right, and here's our matrix E, um, which is just all twos.
And let's say I want to multiply matrix E. Now, of course, I could just multiply matrix D by a scalar to scale it, but I could also do D .* E. And what that will do is it will multiply the thing in column one row one in D by the thing in column one row one in E, and the thing in column 2 row 2 in E by, you know, it just goes through and multiplies each cell by the corresponding cell in the other matrix.
So now if I do that, it'll just double my original 1, 2, 3, 4, 5, 6, 7, 8, 9 matrix. And of course, E doesn't have to be all twos; it could be, uh, you know, for instance, if I have D .^ D, it'll multiply everything in D. It'll basically square every cell in D, and as you can see, that is the case.
Um, so dot star just essentially, um, multiplies every cell by every other cell. Um, you can also obviously add a matrix. Let's say if I just do D + D or D + E, something like that, it'll just add every cell. Um, and then obviously I can still multiply by a scalar. So the equivalent of doing the, uh, just multiplying by the big 222 222 222 matrix would just be D * 2, which just multiplies every cell in D by the scalar 2.
Um, also, you can do a lot of the things that you will have learned in linear algebra on a matrix through MatLab. So for instance, if I want to take the inverse of a matrix, let's take the inverse of D. Actually, D is going to be singular, but I'll show you what happens when we do that, or how you would do it. You just type inv(left parentheses) and then your matrix in there and then a right parentheses.
So I'll actually just type an actual matrix in here. I'll do the 1, 2, 3, 4 matrix, so it's just a normal 2x2 matrix. The first row is 1, 2; second row is 3, 4, and I'll take the inverse of that, and it'll give me this thing. It'll give me the correct inverse. Now I can also take the inverse just of a variable, like if I take the inverse of D.
Of course, D isn't invertible in this case because the 1, 2, 3, etc. matrix just happens to not be invertible. But, um, you can see that I could just pass D as an argument here, and if it was invertible, it would work. But you see that MatLab says warning: The matrix probably, um, doesn't—it can't be inverted.
There are a couple of other operations you can also do on a matrix. For instance, you can transpose it. Let's transpose D, and that just converts all the rows to columns. Uh, so as you can see, uh, this was the first row originally, the second row, and the third row, and now they're shifted to the first column, second column, third column.
Uh, and that will come in handy a little bit later. Later on, um, now another thing is you can create a vector that's just incrementing by a constant. So MatLab makes it really easy to create a vector, for instance, that's just 1, 2, 3 all the way up to 10. All you have to do is I'll assign this to a variable and make it F. All you have to do is have your starting number, colon, and then your ending number.
So I'll start at one, and I'll go all the way to 10, and if I hit enter, you can see that it's just a row vector, and the first thing is 1, the second thing is 2, the third thing is 3, the fourth thing is 4, etc. And, um, of course, I can just transpose that to make it a column vector instead if I just show you the transpose function there.
And, uh, you can also change the increment. So instead of incrementing by one between each column, it increments by, let's say, uh, 0.5. So I'll, I'll re-change the value of F, and I'll make it go from one to, uh, how about one to five, but I want it to step 0.5. And what you do is before the five, you have the step. I'll make it 0.5, and then another colon so that it reads start colon step colon end.
And now you'll see it counts 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5. And as you can also see here, MatLab actually tries its hardest to, uh, make the output neat. But when it can't, it'll just tell you what columns it's really printing out, um, because this is a pretty wide—you know, there are 10 elements and everything, so, uh, MatLab doesn't like printing that on a my small display, but whatever.
And because sometimes it gets annoying if you assign a variable or something like that, and it just gives you some kind of output, and you know what you just input, so you don't necessarily want to see the output again, you can do something pretty straightforward, which just tells MatLab don't print out the result of this expression. All you have to do is I'll just do the last thing again, F = 1 to 5.
Um, all you have to do to tell MatLab don't print out the result of this expression is just put a semicolon, and that means, okay, I still want MatLab to assign the variable; I just don't want it to tell me what—I don't want it to tell me what it assigned the value to. Uh, so this can make your experience a lot cleaner if you have to set a bunch of variables and you don't want to see a bunch of output. Like you want to look back in your log without having to scroll up through all this junk, um, adding a semicolon can really help with that.
All right, so now let's have a look at accessing elements in a matrix. So for instance, if I look back at my matrix D right here, um, and let's say I want to just have MatLab tell me the value at row 2, column 2. Um, all you have to do is type D left parentheses, and then the row number. Now the row number starts at 1—MatLab, in a lot of programming languages, it starts at zero, but in MatLab, it starts at one. So if I want the second row, I just type two there, and then a comma, and then the column number I also want—the column 2—and then a close parenthesis, hit enter, and you can see it gave me 5, because that's at row 2, column 2.
If I do the same thing and pass it 3, 2, okay, this will give me row 3, column 2—be 8—and it is. So this is a pretty simple way to access elements in just a matrix. Um, another thing is, let's say I have two matrices. I have D and I have E, and E happens to be the same height as D, the same number of rows. Then let's say I want to put these matrices side by side in something that's a 3x6 matrix. So I want to make an even wider matrix.
And let's say I want D on the left and E on the right. All I have to do to do that is have a left bracket and then do the first thing I want—D on the left—then a space, and then the thing on the right, which is E. And if I hit enter, now it just joins those two matrices together, um, and it does pretty much exactly what we would expect, and it prints it out.
In addition, you can also do the same thing with columns. So I could do left bracket D semicolon E, and now it'll put E underneath D in a bigger, um, it's going to be a 9x3 matrix, and there we can see it, or a 6x3 matrix rather. There we can see the result is also what we would expect. So now we've looked at some ways to, uh, manipulate matrices in MatLab, play with them, all that kind of stuff.
Now I'm going to go ahead and show you some of the graphing features in MatLab. So first off, let's make a matrix A, and let's just assign it to—uh, let's just make a whole bunch of values in matrix A. We'll make it everything from 0 to Pi, and will increment by 0.1. And this will basically give us—well, let's see, it'll give us about 30 values in our, uh, in our row vector. So it's going to be a big thing, and it's going to show us all that.
I should have used a semicolon, but it's a good thing that you look at this right here. So you can see it's just going up, and it, it doesn't actually go past 3.1, because, uh, if it were to step another 0.1, it would be above pi. Actually, I'll just go ahead and make it up to 2 * pi, so that way, um, there's more data, and it goes up to like 6.2, which is going to be more data, and you'll see why in a sec.
Now, let's make B the sine of every single value in A. So we want to make B the same dimensions—it's just we want every, uh, value in B to be the sine of the original values in A. All you have to do is use the sine function and just pass it the, um, the matrix A. So for instance, if I just did the sine of pi, which is just a constant, it would tell me the answer is pretty close to zero.
This is like 1.2 * 10^-16, but, um, so you can see that the sine function just takes an argument, and I can also give the sine function a matrix, or in this case, it's just a vector, but it's the same thing. So if I have B equals sin(A), every, um, every value will just be the sine of the original value in A. And actually, I should have definitely used the semicolon there, but once again, maybe you want to see what kind of stuff we got going in there, but whatever.
So now we've got a matrix A, which is, uh, all the numbers from 0 to pi, and we've got the matrix B, which is all the, um, the sines from, from 0 to pi basically. And let's say now we want to treat A like our x-axis and B like our y-axis, and since they have the same number of elements—actually, they don't need the same number of elements necessarily, but, uh, it's nice that they do—but let's just go ahead and I'll show you how to plot this data using MatLab.
All you have to do is type plot(left parentheses A, comma, B right parentheses), and this will just treat A like the X values and B like the Y values. And in general, the plot function just takes a, uh, two row vectors. One is the X values, and one is the Y values, and they've got to have the same number, number of stuff in this case, I guess, because that just makes sense. But let's go ahead and hit enter, and you can see it brings up a whole new window right here, and it's got our sine wave, um, which is pretty nice, and it's pretty smooth.
We actually only have, what was it like? Um, it was, it was 0.1 to 2 pi, so we probably only have about 60 values that's on, right? 63 right there, it says. Um, so it's not, it's not a perfect sine wave, obviously, but it's a real-life application like we have a set of data, and we want to plot it. Now another thing we can do is we can, uh, go ahead and generate something called a, uh, surface graph. And a surface graph, we give it a matrix, um, where the rows are one axis, the columns are another axis, and then the values in each cell at a certain row and column is the Z-axis, and it'll just plot the, um, surface area.
So to give you a good example of this, I'm going to have to teach you how to do something new with a matrix just so you can play around with it a bit. But let's say we want to make a square matrix where all the, um, either all the columns or all the rows are the sine of their row or column. So that way, if you picture what this will look like, it'll basically be a whole bunch of the same row again and again and again and again.
Um, but what this will be is it'll be like a sine wave pushed backwards to be like a blanket that's kind of taking a sine wave shape. And it's not going to—it's going to still be like it's going to be like a two-dimensional pushing the sine wave backwards. It's not going to be a bunch of, like, mountains or something like that, but it'll still be neat to see what this looks like.
So we already have our function, or our vector B, which contains like 60 samples or so. Um, so let's just say we want to take B and we want to repeat it, let's say, 60—63 times—so that way we have a square matrix. Well, there's a function in MatLab called repmat, and you basically just give it, um, two numbers, and it repeats the vector or the matrix even that you give it a bunch of times, and it treats the thing you give it as if it were, um, a column.
So unfortunately, our vector B is actually a row, but all we have to do is type B = transpose(B), sorry, and it'll make it into a column. And I'll just set C equal to transpose B, and now C will have all our sine values right there. You can actually see that now that it's a column, it prints out much neater because it just goes down and down and assumes you're going to scroll. So you can actually see this is what our data in C looks like.
Now let's say we want to use the function in MatLab to repeat this only, let's say, uh, how about 10 times? So all you have to do is let's say set another variable D equals repmat, and then the first argument is the thing you want to repeat, the second argument is the number of rows you want to have—I'll just make it one times the original—and the third argument is the number of columns, which we want it to be 10 times as many.
And now I'll go ahead and hit enter, and D will just have a whole bunch of columns. You can see it prints out some of the columns, and then it prints out the next columns after that because that's how MatLab prints out data, but it's got 10 columns, and each of the columns is exactly the same thing.
So now let's say we want to take this data and we want to make a, um, a surface graph where the column number is one axis, the row number is another axis, and the value in the cell is the Z value—it's another axis. So all you have to do is type surf and then pass it the variable or the matrix—in this case, it's D—and we hit enter. And now I'll go back to my draft window back here, and you see it actually graphs what I was describing.
It's a sine wave, and then it's replicated going back 10 times, and there are probably 60 of these little squares going up on the matrix going whoop, whoop, whoop. And in fact, you can see it actually goes up. You can see the axis here; it goes up to like 80, so—and the axis here only goes up to 10 because there are only 10 samples—but that's fine because we're just pushing it way back.
So here we've created a surface graph. Of course, um, in this case, I didn't have the best data. I just created a matrix that had data that I knew would be, that would look like something neat to show you. Um, but in a real-life application, you might have a matrix that you actually want to graph this way, and, uh, with the surf function, you can, um.
Now, two other neat tricks I just want to show you real quick, and these are by far the most useful things for me, uh, that I find is, um, changing the format. So if you recall from earlier, we did like inverse of 1, 2, 3, 4, and it gave us this thing, and you have 2.1, you know, -2.1, 1.5 and -0.5. And that's okay—I can understand what that is. This is like one and a half; this is a half.
Um, but if it was something like 88888 and I wanted that in a fraction, MatLab just—it would seem as if MatLab was just not giving me something I needed. It's not giving me the exact answer; it's just giving me a rounded version. And you can actually just make MatLab give you the answer in terms of a rational value, and all you have to do is type format rational, and then go ahead and hit enter.
And now, if I rerun that last command just by dragging it in from my history and hitting enter, you'll see that now it comes out -2, 1, 3 halves and minus a half. Um, so this is useful in some cases; it's annoying in other cases.
So to turn it back to the original, you just type format short, and it's short because it's only showing you like four decimal places. If you do format long, it'll show you more. So now if I do the same thing, you see, that's just bad—why would I want that? I don't know, but I like the short format.
Um, another thing that's nice is in terms of another thing you can do with formatting is changing, um, how it spaces things out. So right now, you can see there's a big amount of space—there's like a new line there, a new line there, a new line there, uh, lots of spacing there. All you have to do to turn this off is type format compact, and run the same inverse thing, and you see there's much less space.
Or format loose, and we run the same inverse thing, and it's, uh, all together again. And by the way, the way I'm running my previous command is just by hitting the up arrow that goes back in your history, and the down arrow goes back down in your history. So that's an easier way than going to your history here and dragging in, but both of those are legitimate options.
But anyway, uh, I hope you learned something in this video. I certainly learned a lot making it, and I anticipate learning a ton of, uh, stuff about MatLab in the future, and maybe making a couple more videos in the future about it if you guys would like that. But anyway, uh, thanks for watching, subscribe, and goodbye.