See how to create some basic types of matrices, including ones, zeroes, and random values.
- [Josh] Often it can come in handy to create large matrices full of zeros, or ones, or random values. We'll put that to use a little bit later in the course. But real quick, let's just see how to create these matrices. Let's go here to the command window, and let's clear the workspace with "clear all", and then I'll clear the screen. And I'm going to go ahead and comment out, the section of code we have written here. I'll highlight it, and say "Comment". If you're on Windows, you can also hit ctrl-r.
Let's give ourselves a couple more lines. Let's add in another comment here, and I'll say, "Create some standard matrices". One that often comes in handy is to create a matrix all full of zeros. So let's create a new variable, I'll call it, "myZeros" and a super easy function, it's just called "zeros()" and then however many rows and columns of zeros you want. We'll do a 20 by 30 matrix. We don't really want to see that on screen, so I'll put a semi-colon.
We can do the same thing with the ones. If you do "myOnes" and then set that equal to the function "ones()" that's 100 by 100, we'll have a 100 by 100 matrix of ones. You may ask why you would ever want to do this. It can come in handy for performance considerations when you need to, say, populate a large matrix in a loop. It's then much, much faster to populate that matrix than it would have been to increase the matrix size each time through the loop. But we'll see this explicitly in a video later on. Lastly we can also create random matrices if we wanted to.
We'll say, "myRandomInts" and lets create a matrix full of random integers. We can use the function, "randi()" for that. And let's create a five by six matrix of random integers. If you ever want help on these functions, you can always double-click them, right-click, and say, "Help on randi()" and that will bring up the context help. You can see here there's lots of different variations on it. One thing that I find really helpful is if you scroll down to the bottom of the help.
There are some examples and it will also give you other functions at the very bottom here that will perform similar operations. So, for example, "rand()" or "randn()". "randn()" will give you a normally distributed matrix of random values. So if I'm ever not sure of which function I want, often I can look up a similar function and find what I'm after. So, let's go ahead and run our script and we'll see that our three matrices were created over here in the workspace. If we double-click on one of the matrices, It'll open it up in this Excel-style viewer.
We can see sure enough, we've got a matrix all full of ones. And there's our random integers, and here's our zeros.
- 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