## The Complete Linux Course: Beginner to Power User!

Get started using Ubuntu Linux with this FREE course!

## Learn Matlab Episode #12: Sound Processing

How do you open and process sound files in MATLAB?

## Learn Matlab Episode #11: Subplots, 3D Plots, Labeling Plots

Creating subplots, 3-D plots, and labels in MATLAB

So in this lecture we are going to talk about more plotting. We’re going to talk about subplots, so that’s putting more than one plot into the same figure, we’re going to talk about three-dimensional plots, and we’re going to talk about how to label plot, so you want some field for the x-axis, the y-axis, and the title. So, let’s go into subplots. So suppose I have some
function of X, so let’s just say Y = sin(x), then I have another function which is just Z = Y + randn(1,100); Ok, so I can plot these individually, I can plot them together, I might want to stack them up or something to see if there is a correlation between them, so the way we do that is with subplot. So we call a function called subplot and it takes in three values. Ok, so the three values are the number of rows in the subplot, the number of columns in the subplot, and then which subplot you’re about to plot. So since I have two subplots let’s say I want two rows in one column and I want to set the first plot. So I just called subplot(2,1,1); and I say plot(X,Y); Ok, so now my figure has X,Y in the first row and first column. So now I do subplot(2,1,2); because I still have two rows in one column, but now I want to plot something in the second slot. Here I’m going to plot X and Z. So now if I look at my plot you can see the two plots stacked up on top of each other instead of overlaid. So the next thing we’re going to talk about is 3D plots. So suppose we have some two dimensional data set I’m just going to make it random noise. Okay, so we look at the matrix Z, we see that it’s 100 by 100, so the rows you can think of as the x-axis, the columns is the y-axis, and then the value as the third dimension. So how would we plot this? So there are a couple ways you may have seen already from your math courses, so we can plot an actual 3D plot so sort of how you would draw a cube on a two-dimensional piece of paper, or you could use what’s called a contour plot which draws lines where the value of the function is equal. So we’re going to do both of those. The first one is a function called surf, so you pass in the matrix and so you can see the x-axis and the y-axis both go from zero to a hundred. If you look up the documentation you can set the x-axis and y-axis manually, and then the height is the value of the function. So what I did just now as I clicked on the this thing that sort of looks like a circular arrow and so what that allows me to do is it allows me to rotate the surface plot, so now I can look at it from different angles which could be very useful if you’re doing data analysis. The other type of plot that we talked about is the contour plot. So the simplest way to call that is with the one argument and that generates just a 2D visualization of your data, the red is the really high values, and the blues the really low values. So the third thing we’re going to talk about today is how to label plots. So far we’ve only seen plain visualizations, so let’s go ahead and plot the sine function again. Alright, so I want to give this plot a title, all I do is use the title function title(‘A plot of sin(x)’) Ok, so now if I look at my plot you can see it now has the title A plot of sin(x). Now I want to label the x axis I’m going to label it with the very unoriginal label. Ok, so now you can see the x-axis is labeled and you can do a similar thing for the y label as well. Ok, so that’s pretty much it for labeling, suppose I want to label a subplot. Ok, so now I’m going to create another function, going to subplot(2,1,1), plot(X,Y), title(‘sin(x)’) Ok, so once I do that the first plot, first subplot gets the title sin(x). Now I call subplot(2,1,2), I plot(x,z) and I call title again so it’s the same function but it’s stateful so it knows that I called subplot earlier and that it corresponds now to the second plot. Ok, so I call title again and now my second plot has the title cos(x), and my first plot has the title sin(x).

How do you save & load data in MATLAB?

## Learn Matlab Episode #9: Data Analysis Plots

What are the best plots for data analysis in MATLAB?

## Learn Matlab Episode #8: Plotting Data

How do you plot data in MATLAB?

## Learn Matlab Episode #7: Data Types

Data types in MATLAB

## Learn Matlab Episode #6: For Loops, While Loops, If/Else Statements

Loops in MATLAB

So in this lecture we’re going to talk about for loops, while loops, and if-else statements in matlab. The first thing I’m going to talk about is the basic syntax of a for loop. So the basic syntax of a for loop is for i= some array of numbers, so it can be anything. So, for argument’s sake i’m going to use just a bunch of random numbers [1 3 5 4 10 7] and then I’m just going to display them to the output. Ok, so all that does is it sets i to the first number for the first iteration, it sets i to the second number for the second iteration, and so on. We’ve seen before that matlab can create ranges with the colon though which is probably a more common use of the for loop, so you want to count from one to a thousand or 1 to 10 and so the way you would do that is for i=1:10 which actually in MATLAB is generating the array 1 2 3 4 5 6 7 8 9 10. So I’m just going to display(i) again and you can see that it goes through every value of i. So now that we have the basic for loop syntax in matlab, what I want to do is go through a little more complicated example. You’ve heard of a concept called the mean squared error. If you haven’t let me show it to you on Google. So the mean squared error is if you’re trying to predict something you’re going to have some error for all the data points between the true values and the predicted values, and so the mean squared error basically takes the difference between your prediction and the actual value squares them all, sums them together, and divides it by the number of points. So you can see how we might be able to use for loops to implement that. So now since we don’t have any data or any error we’re just going to pretend that we do. So I’m going to use the rand and function to create an array of size 1000 by one, these error points are going to be normally distributed which is often an assumption that we make any way with statistical models. Ok, so now I have a 1000 length array of errors which represents the difference so i don’t have to calculate the difference in this equation, and so now we can go ahead and calculate the error. So the first thing we want is the sum of square error, right, so that’s just the sum of all the individual elements. So I can do that by first initializing the sum of squared error to zero, and I’m going to do a for loop from 1 to 1,000 to accumulate the sum of squared error being a square of each individual error. Ok, and so now that I have the sum of squared error I’m going to divide that by 1,000 to give me the mean squared error. So one thing to note about MATLAB is that for loops, while loops and if statements are actually very inefficient, so you don’t want to use them unless you absolutely have to, and so I want to demonstrate how that is the case. So we have this mean squared error example, I’ve pre-written the code, and I use a statement called tick and talk to time how long it takes. So you basically you start your code with tic, you put all your actual code after that, and then you put a talk at the end and then it will
tell you the elapsed time it took to run that code. So I’ve already pre written it so i’m going to paste it out here, tic t-i-c, toc t-o-c, ok. So that took .005668 seconds. Now if we look
again carefully at the mean squared error we notice that we want to square each element and then sum them all together, and so that sounds a lot like the dot product, right. So if you take the dot product of a vector with itself you’re just squaring each element and then summing them altogether. So if I wanted to do the mean squared error equation in a more compact way, what I
might want to do is the dot product between e and itself which is e transpose times e as a matrix and then divided by a thousand, right, and so that gives us the same answer as before, and so if I put a tic and a toc around that I get .002896 seconds. So it’s significantly faster than using a for loop. Ok, so that’s for loops, now we’re going to talk about if statements. So what I want to do is I want to visit I want to revisit the is even problem that we talked about in an earlier tutorial. So if you recall what I did was I created a function that returns one for all the entries of X that are even and return 0 otherwise. So you could do this with if statements let’s say X = 1:10, alright and we want to say Y is initialized to a an array of zeros, and we will do for i=1:10 if mod(x(i),2) == 0 then x is even, right. So, that means we’re going to set Y(i) = 1; else Y(i) = 0. So we don’t really need that else since Y(i) is already 0, but I’m including it so you know what an else looks like. Okay, so now if we look at Y has the ones where we expect it which is where x is even. Ok, so now let’s do a similar example also using for loops and if statements. So the example is the problem is let’s say I want to sum all the elements of an array that are divisible by 3, so let’s just use the same X as before, let’s reinitialize Y to all zeros, sorry we don’t even need to do that, let’s set S = 0; and so now I’m going to loop through all X’s again for i=1:10. So if mod(X(i),3 == 0 so if it’s divisible by 3 we want to sum that element. S = S + X(i); and now this time we’re not going to include the else. So is 18 which is equal to 3 + 6 + 9 which we expect so that’s the correct answer. Now of course there is another way to do this, so if you only want to look at the elements in X so notice how whenever we use i we’re actually saying x(i) we can just do this for x=X right because the little x is going to just go through every element in the big X. We were going to start again so we’re going to reinitialize S = 0, say for x=X,if mod(x,3) ==0, S = S + x; end. So if we look at sum again we still get 18. So now let’s look at another problem. So suppose we want to find an element within a matrix. So suppose again we have the same X and I want to find the number 8, so I can use a while loop for that. So I can say I can set a variable called found = 0 meaning false, and then I can say while not found, so I’m going to initialize an index i = 0, gonna say while ~found, i = i + 1 if X(i) == 8 which is the thing I’m looking for. disp(‘I found it\n’); end Ok, so I made a little mistake there we went out of bounds because i went to 11 and the size of X is only 10, so what we did was we forgot to set found to true after we found x(i) == 8. So I’m going to reset this code, i = i + 1 if X(i) == 8, disp(‘I found it!’); set found to true found = 1; now end the if statement, right. So now we go up to 8, printout i found it, and then we quit the while loop. So while loops only go until the condition after the while is false, so it will continue to go while it’s true, and then when it’s false it will stop. Another way we could do this though is we could again use a for loop. So generally speaking whenever you can use a while loop you can also use a for loop, it’s just the syntax that will change and a little bit of the structure of the code. Suppose I want to look through all of X again, so I want to say if X(i) == 8 disp(‘I found it!’) So now this works but there’s a problem right because in the original while loop we would only go up to 8 and then we would quit, versus this for loop goes from 1 to 10 so that’s inefficient because it’s doing extra steps. So what we can do to avoid that is use the break statement. So just to prove to you that it works i’m going to print out for i=1:10 so again if X(i) == 8 disp(‘I found it!’); break; and so now you see I only goes up to 8. It finds 8, it breaks, and then it doesn’t go to 9 and 10.