Beginner to Advanced Matlab Tutorial [Complete Course]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there scott here your video is going to start in just one moment i just wanted to say thank you for stopping by my channel the video that you're about to see is part of a series of videos all of these videos are educational they're teaching you a strategy or they're teaching you about a new tool or they're walking you through a campaign or somebody's delivering insight all of the people that teach over in these videos some of them are me but a lot of them are other individuals who are subject matter experts in their field i hope that these videos will be useful there's a variety of sales videos marketing videos videos that highlight and discuss different tools and technology as well as videos that discuss high level as well as granular strategy these are meant for individuals that are looking to level up in their own career or if you're going down an entrepreneurial road and you want to understand how to build a business from the ground up a lot of these videos can help you as well if you enjoy the video and you watch the whole thing if you got some value from it which i really hope you do please like obviously hit that subscribe button it means a lot but also i want you to check out two other free resources there's a newsletter a bi-weekly newsletter called roi overload that basically highlights the best the latest the greatest the tools strategy insights articles case studies for sales marketing entrepreneurship and if newsletters aren't your thing then you can also check out the roi overload medium publication again it's a free resource that allows you to read case studies learn from people that have done it before a wide range of authors contribute to the roi overload meeting publication again link is in the description of this video i hope you enjoy these resources i hope you get some benefit out of them that's all i got here's your video hey guys welcome to my matlab tutorial today we're going to talk about what is matlab how do you get it various add-ons and toolboxes you can purchase to extend the abilities of matlab and generally how how you can get help using matlab so the first thing i'm going to show you is just i'm going to google matlab and right at the top you see matlab's official website so that's where you can go to get matlab and learn a little bit more about it just by reading on your own to give you a short introduction though matlab stands for matrix laboratory so if you've ever programmed before it's a little bit different because matlab is mostly for math and all the variables are matrices even scalars are matrices matlab is usually used in academia and research so if you're taking a university course which involves numerical computing uh you'll probably use matlab if you're doing research and you're analyzing data your professor probably already uses matlab and everyone in your lab as a result we'll be using matlab matlab is used in fields of engineering science finance and economics at its simplest matlab basically does calculations so it's like a it's a very fancy calculator on top of that though you can write code to evaluate a complicated formula you can plot equations and data and you can implement algorithms okay so let's talk about getting matlab so i've already opened the website if you scroll down to the bottom near the bottom you can see a section called try or buy so you can try matlab for free probably a trial it lasts 30 days let's check it doesn't say you have to enter your email but it's a it's a limited time trial so it won't last forever if you want to buy matlab there are several options so the standard matlab you probably don't want to use it's um over two thousand dollars if you're part of an educational institution you can use the education tab and get matlab for five hundred dollars the home version is just for personal use that's 149 and the student version is 49 so you probably have to go through some process to prove that you're a student another way that you can probably get matlab is if you are a student at a university there are options that the university has for you to get matlab so you have to consult with your university's resources such as the website or the bookstore to see if you can possibly even get it for free for this tutorial i'm going to be using the 2014a version of matlab so one thing you'll notice is for the the home version and the student version is they have these add-on products that you can get for a price so add-ons are also called toolboxes and they have functionality and code that can be useful for you in your specific field of work so for example if you do bioinformatics you may want to purchase the bioinformatics toolbox so you can click on the links and get an idea of what's included in the packages maybe you do bioinformatics and you don't really even need the functions that the toolbox comes with so you might not want to purchase it but in general you can browse the documentation for free so you can see everything that you would get if you did purchase the toolbox it's essentially an api so you can look at any function see what the input and the output is and decide if that's useful for you the next thing i want to talk about is how to get help with matlab so you're already taking this course and you're going to learn a lot about matlab here and when you start coding there are probably going to be some very specific things you want to do that you don't know how to do yet so one place you can go i clicked on the community tab on the matlab website and so you can see there's a a community of people who use matlab who post on this site the file exchange i've used before people post useful functions and code that you know you may need in your projects that can be useful there's also a section called matlab answers where people ask questions and you can answer them or you can ask a question and someone else can answer you personally i would recommend just searching your question on google because they've already indexed all this stuff you could potentially put your question in the search box on the matlab site as well though and you might on google also get stack overflow results so it wouldn't only be matlab central which might be helpful welcome to this matlab tutorial this matlab tutorial is going to be on the basic syntax of matlab so i've already opened matlab and this is generally the screen that you see most of the time so the big main window is called the command window you'll be working here most of the time you can view your variables in the workspace on the bottom left and you can view your current directory on the top left and so that will tell you where in the file system you currently are so if you want to open a file from there you don't have to type in the absolute path you can just type in the file name directly or if you save a file that's where it will go so we call that the working directory and you can change the working directory to keep all your projects separate so you can go into the path up here and you know type in another path if you want to use uh the path for your particular project so in matlab there are three basic types of numbers that you'll be working with um or variables that are all actually matrices so you you have um scalars which are just plain numbers you have vectors which is kind of like a list of numbers and then you have matrices which are tables of numbers um so as i've mentioned before in matlab all these variables are represented as matrices so if i do something like x equals 5 you see that in a in a regular programming language you might just think of this as x equals five but i can do something like check the size of x and i see that it's actually a one by one matrix i can create a vector okay we use square brackets and you don't need commas but i'm going to use them and so that creates a vector with the elements one two three so if i do size of v i see that it's a one by three vector or you can also call it a one by three matrix so this brings up some other interesting points about how matlab syntax works i mentioned that you don't actually need the commas so we can just do that and matlab knows that we want a 1 by 3 matrix with the elements 1 2 3. so you also notice that i added this semicolon at the end of the line and what that does it's easier to see if i just don't do it so it prints out the last thing so if i don't use the semicolon it's going to print everything out from the previous command but if i do use the semicolon it will suppress the output so that's why i didn't put a semicolon after size v if i did i wouldn't see anything now we're going to go on to look at matrices so it's very similar to creating a vector except we need to have a way to separate each row so that's done with a semicolon and i'm not going to put one at the end so we can see the output and so what that does is it creates a 2 by 2 matrix with the elements one two three four in a variable a right if i do size a it's two by two so those are the two things we've learned about initializing vectors and matrices is that you can use commas to separate columns but you don't need the commas you can just use spaces and to separate rows you can use semicolons another thing is that when you're when you're working with matrices and vectors and you're multiplying them and stuff like that the the dimensions of the vector are very important so typically you know you'd work with a column vector not a row vector like the one i just created and so we can easily transform the previous vector which was a row vector into a column vector using an apostrophe and so that does a transpose so you see now that v used to be a one by three vector and now it's a three by one column vector you could have also initialized it using semicolons of course okay so the next thing i want to talk about is how you can access elements of a matrix typically that's done using parentheses and then indices and commas in between so if i want to in general access the if row and the j column it would be a parenthesis i comma j parentheses so if i want a 1 1 for example i would do that if i want a12 and so on a21 another interesting thing you can do is access a vector within a matrix so let's say i want the first row of the matrix a i can use the colon syntax to get the first row so i'm going to put a 1 or i choose the row and then i'm going to put a colon in the choice for column which means return me all the columns in row 1. so if i do that i get 1 2 which is the first row of a similarly i can do i can use the colon syntax to get an entire column of a so let's say i want to choose the second row or sorry the second column so let's say i want to choose the entire second column i would do that and of course if i do a colon in both parts of a it'll just return me the entire a so let's create a bigger matrix now so i can better illustrate my next point all right so i just created a four by four matrix so the colon syntax can also be used to access ranges so if i want so if i look at a it's 1 to 16. if i want the part of a that has so a sub matrix that has the 6 7 10 11 i could access that part by choosing the second and third row and the second and third column if you've programmed before usually when you're working with vectors matrices arrays there's zero indexed in matlab is one index so a11 returns me the first element it's not a zero zero one other cool thing that we may use later is the colon syntax can be used all by itself it actually just creates a range so if i did something like 1 colon 10 it gives me back all the numbers from 1 to 10 and so say i want to assign this to a variable let me check the size of w so that creates a row vector with the elements 1 to 10. it's uh simpler simpler to do than than typing 1 through 10 manually right so just to extend this a little bit we aren't limited to only scalars vectors and matrices in matlab you may have heard of a data structure called tensors and so those are multi-dimensional matrices so it's not a table it's more like a a cube or a hypercube so you can instead of having a two by two matrix you can have a two by two by two tensor so in this matlab tutorial we're going to go through some basic arithmetic in matlab we're first going to talk about the usual operations you see in programming which are plus minus multiply and divide and also the use of parentheses so i'm going to first create some matrices for us to use okay so suppose i want to add two matrices that's very simple a plus b if i want to subtract two matrices it's a minus b if i want to multiply two matrices that's a asterisk b so one thing to note about multiplication is this is pure uh matrix multiplication which means the inner dimensions of the matrices of a and b have to match so for example if i create another matrix which is if i create another matrix which doesn't have the same number of rows as a has columns it's not going to work so say so the size of b2 is 3 by 2 and the size of a is 2 by 2. so if i try to multiply a times b 2 i'm going to get an error that says inner matrix dimensions must agree so this does mean though i can do this right so if i transpose b2 it's two by three and so a two by two matrix times a two by three matrix will give me a two by three matrix so you can also divide matrices with a forward slash so but if you remember from linear algebra there isn't really a matrix division like that so if you want to know what it means you can look it up on the internet there's a forward slash division for matrices and there is also a backslash division um which we are going to cover in a later tutorial by the way if you want to divide two numbers that's very simple if we use the right slash for that so the next thing we're going to talk about is order of operations it's generally what you'd expect anything in parentheses happens first and then multiplication and division and then addition and subtraction so if i have a plus b times c i would get that so if i did b times c and i assign it to a variable d and i add that to a i get the same thing so you can confirm that the multiplication is happening first if i wanted the addition to happen first i would put parentheses around them so we can confirm that what happened in the parentheses came first so another thing you might want to do when you're implementing an algorithm or writing some code is element by element operations so we've looked at matrix multiplication which kind of does you know if you're looking at row i column j of the output that actually does a dot product between row i of the first matrix and column j of the second matrix something we want to do often when we're coding is element by element multiplication and so that let's just look at what we have for a and b again so that's pretty simple in matlab you just put a dot before the multiply sign and so it multiplies each element by each corresponding element in the other matrix this also works for division so with dot slash so if you remember from your linear algebra classes there are some different types of multiplication that you can have when it comes to matrices and vectors the first one we're going to talk about is the inner product if i have x equals 1 2 3 y equals 4 5 6 and i want to do an inner product between these two vectors one way to do that is simply by using what we already know so x is a one by three vector and y is a one by three vector and i want the result to be one by one which is a scalar that means i can transpose y and multiply x by y as matrices right and so that would be 1 times 4 plus 2 times 5 plus 3 times 6. another way you can do the dot product in matlab is by using the dot function so we'll we'll cover functions more in a later lecture but we'll introduce you to some today so another product you might be interested in is called the outer product and so that would be just the opposite of what i did before so instead of transposing y and leaving x alone i'm going to transpose x and leave y alone so that would give me a three by one vector times a one by three vector and so the result would be a three by three vector right so that's the outer product and there is no function for outer product in matlab and so one thing to note is that usually when we represent vectors when you're reading a textbook or sitting through a lecture the vectors are column vectors so they have many rows but only one column and so that's kind of the opposite of what i've done just now and so for the inner product you usually see x transpose times y and then for the outer product you'd see x times y transpose so just the opposite um since i've been using row vectors lastly there is the cross product right and so the the cross product is the magnitude is the magnitude of the first vector times the magnitude of the second vector times the sine of the angle between them and then you use the right hand rule to determine the direction of the result right so dot product or inner product is not a vector so it doesn't have a direction but the cross product gives you back a vector so that's pretty simple to do in matlab also it's just a function called cross so in this tutorial we're going to talk about matlab functions and constants so built-in functions and constants to be specific and then how you can define your own functions later on so by functions what i mean is mathematical functions like sine cosine tangent exponential log and square root and so those are pretty much what you'd expect if you've ever coded in any other language before so i can do sine of zero gives me zero i can do cos of zero gives me one tan of zero of course would be also zero so let's look at these trigonometric functions for a little bit so one thing is that because matlab works on matrices i can actually pass in more than just one number into sine so suppose i use the matrix we had before one two three four if i pass that to sine what that does is it calculates the sign of every element of a and returns a matrix the same size as a so i can do all of that separately of course just to show that it is indeed doing that individually for every element and so all matlab functions generally work like that some other functions you might want to use is the exponential function that's exp there's the log which is the opposite of the exponential so if i do log of exp of one i should get back one um i can do square root sqrt okay so those are some very simple elementary functions in matlab there are also some built-in constants into matlab that you should know about so for example pi that's just pi so if you do something like have a variable called pi and you assign it to something else now i cannot use the original variable pi anymore so what i would do if i want pi back is i would go into my workspace and delete pi right so now if i type pi again it's now back to original pi generally i would avoid uh overwriting built-in constants so one weird thing about matlab is it doesn't have a variable e so if you want the variable e you actually have to do exp of 1 which is e to the power of 1 which is e another important constant is i so if i do square root of minus 1 i get 0 plus 1 times i i can also just type in i and it would give me the same answer so you can also do things like i times i which should give you minus one or you can do i squared um so you do exponents using the hat symbol so that also gives me minus one so there are also some important functions for initializing matrices that you should know about so the first one is the i function so you've seen i in the sense that if i have a matrix a then a times i should equal a and so i is the identity matrix and it's a square matrix with ones on the diagonal and zeros everywhere else so suppose i want to create a three by three identity matrix i could do something like this um which would be a little bit inefficient right or i could do i three which gives me the same thing another thing you might want to do is create a matrix of all zeros so that's just the function zeros so that gives me a three by three matrix of all zeros if i want to specify different dimensions so i don't want it to be a square i can do i can pass in multiple dimensions there's also a function called ones which does a similar thing it creates a matrix of all ones you pass in the size the same way so the next thing i want to teach you guys to do is how to define your own functions so generally you want to for every function you create you want to put it into a new file so i'm going to go new function so it gives me some boilerplate code that i could use and this is good because it'll help me explain the syntax of a function to you so when we create a function it starts with the word function if your function returns things you can specify the output arguments over here if there are more than one output argument you can specify it as a as an array if there aren't any output arguments you can get rid of this part completely and if there's only one output argument you can get rid of the brackets so let's suppose i want to define the hyperbolic sine function which matlab probably already has but we're going to do it anyway so it's going to take one input argument called x and take an output called y so matlab has some useful syntax highlighting it'll give us a warning you know stating input argument x is unused since we haven't written any code yet and then it's returning a value y but we haven't set y to anything yet so let's save what we have so far you can do uh command s or go to file save so we'll save it so one thing to note is you have to save the function name has to be the same as the file name and so like i mentioned before matlab already has its own signage so we don't want to overwrite matlab signage so we're going to call our sign h something else i'm going to call it my sign h i'm going to rename this file to my signage all right so now i'm not getting that warning anymore okay so we're going to define our own hyperbolic sign which is defined as the exponential of the input minus the exponential of the negative of the input and then taking one half of that so let's do that so inside the function i have to set y remember because i was getting a warning that y was not set spx p of minus x and then remember i want to put a semicolon here so that i don't see the output of this expression i'm going to get rid of this comment because this function's pretty trivial but in general if you have a complicated function you should probably comment it so you remember what you were doing later on or whoever else is reading your code will understand what you're doing and so you can write comments using the percent sign to comment code so i'm going to save that i'm going to use this function so my sign h let's say one zero right so we can see from this image that hyperbolic sine crosses zero and then it increases as x increases so that's what we should be seeing remember that matlab works with matrices though so if i wanted to i can say x goes from zero to 10 and i want to calculate the hyperbolic sign for all of those values i can just pass in x and i'll get back hyperbolic sign for all of those values and so that's how you define your own function probably yours in the future will be more complicated than that but hopefully that was a useful simple example so we're going to do one more function example here it will illustrate some new concepts so i'm going to instead of going clicking the new button and choosing function i'm actually going to right click on the workspace and i can see an option to create a new file and i can create a new function that way so i'm going to call this new function is even okay and so you can see matlab has already um replaced so before when we created a function from a new file it called it untitled but now that i've given it a name to start matlab already knows to change the function name to that name okay and i'm only going to have one output argument again so i can get rid of these brackets the input argument is going to be x i'm going to add a comment returns one if x is even okay so if you've done programming before you've probably heard of the modulo operator although i haven't mentioned it in this tutorial series yet it does exist in matlab so in matlab because the percent sign is used for comments we do not use the percent sign for modulo if you've done that in c or java before so instead we use modulo by calling a function called mod do mod 2 1 sorry 2 2 3 2 so basically it returns us the remainder okay so this should be very simple we we want to return 1 if x is even so we know we want to mod 2. so y equals mod x two okay but we have a problem here because when you take the modulo of an even number with two you will get zero but if you take an odd number modulo two you get one so that's kind of the opposite of what we want but what we can do is we can use the utility operator which does a logical negation so tilde zero gives us one until the one gives us zero so what we want is to put the tilde in front of the mod okay and so now if i call is even [Music] one two three four five we should get ones in the spots where the input is even which we do so for two and four we get one for one three and five we get zero so in this matlab tutorial we're going to talk about some basic linear algebra in matlab the first thing i want to talk about is how to solve linear equations so in general these are in the form of ax plus b where a is a matrix x is a vector of the variables you want to solve for and b is a vector of constants so if you remember from linear algebra a system of linear equations is when you have more than one variable and the same number of linear equations so in this example i'm looking at right here the variables are x y and z and because there are three of them i have three different equations so one way of writing the problem is just writing the equations out explicitly like this or you can write it out in matrix form so in this example a would be three two minus one two minus two four minus one half minus one okay so that's a b is one minus two minus two okay so when you're working with equations like this you might be tempted to do just simple algebra so you could say x is equal to the inverse of a times b because mathematically that works so i can do that oh and right i have to make b a column matrix so a times b sorry i use the wrong b so inverse a times b so i get 1 minus 2 minus 2 which is the answer given on wikipedia so sometimes when you try to do something like this matlab will give you a warning this is a very inefficient way of calculating this answer this matlab already contains algorithms to solve this problem so if you remember from before when we were talking about operations we were talking about the division operations and how there is a forward slash division and a backslash division so this is the use case where you'd want to use the backslash division i mean if you kind of think about it if these were scalars you know x would be b divided by a but since they're matrices we write the answer like this so a backslash b so kind of it's kind of like a is underneath the b right and so if i do that you can see that i get the same answer as when i did inverse of a times b so that's how you solve systems of linear equations in matlab so now we're going to talk about some other linear algebra concepts that you can do in matlab one of them is taking the determinant of a matrix and so that is just a simple function called d-e-t okay so remember a was three two minus one two minus two four minus one half minus one from the last example we can also compute the eigenvalues of a matrix so i a e i g and now you know when we compute eigenvalues we usually want the corresponding eigenvectors so one interesting thing about matlab that differs from other languages is that what is returned by the function is not set in stone it's dynamic so if we look at the documentation for the eige function we see that if there is one output parameter it returns this little e and the matlab documentation says e contains the eigenvalues of the matrix a but say i want the eigenvectors also and so you can see these different function signatures so if i assign the return values of i to two different variables i can get back let's read here a diagonal matrix d of eigenvalues in a matrix v whose columns are the corresponding right eigenvectors so the eigenvalues are going to be returned in a matrix called d and the eigenvectors are going to be the columns of the matrix v so let's try that equals i k right so we get a diagonal matrix d which has the same eigenvalues as before and then the eigenvectors are all in v and so let's just confirm that the definition of eigenvalues and eigenvectors holds so a times v should equal v times d right and so here's another thing interesting about matlab is that if we want to check if two things are equal we can generally use the equals equals as we do in regular programming so one equals equals one is one which means true one equals equals zero is false um so if we do a star v equals equals v star d we get false which is surprising because they should be equal given that that's the definition of eigenvalues and eigenvectors but we have to remember that there's always in programming some round off error so if we subtract a times v minus v times d we should get back a very small number right so we do that we see 1 times 10 to the minus 14 which is indeed very small so for all intents and purposes they are still equal even though equals equals returns false now so that brings us to the next thing i want to talk about which is so we have this matrix a times v minus v times the which kind of represents all the differences in those two matrices and so we want to ensure that all the values are small now there are nine different values in this matrix so it's kind of hard to tell how big or small it is collectively so one thing we want to do sometimes when we have vectors or matrices is we want to calculate the distance from the origin and so we can do that with vectors um with matrices we call that the norm and so typically all the elements so in in a euclidean space for example it's all the elements squared added together and then you take the square root so in matlab there's a function called norm that does this so let's just see what that gives us so it gives us one very small number so 5 times 10 to the minus 15 which we again can use to confirm that um it's very close to zero so a times v is very close to equal to v times d and if you remember from your math courses there are different kinds of norms so i talked about the euclidean norm which is a norm two we can actually do a p norm which is so instead of taking the square and summing them all together and taking the square root we do every element to the power of p sum them all together and take the p of root so and that's just the second argument of norm so if i put a 2 there i should get the same answer because p equals 2 by default so you can't use p equals three you can only use p equals one two infinity or fro and so one would be the manhattan distance hey guys so in this lecture we're going to talk about for loops while loops and if and 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 index equals 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 and then i'm just going to display them to the output okay 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 one to ten and so the way you would do that is for i equals one colon ten which actually in matlab is generating the array one two three 4 5 6 7 8 9 10. so i'm just going to display i again and then 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 i'll 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 and 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 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 anyway with statistical models okay 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 um and so now we can go ahead and calculate the error so the first thing we want is the sum of squared 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 one to a thousand and i'm going to accumulate the sum of squared error being the square of each individual air okay and so now that i have the sum of squared error i'm going to divide that by 1000 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 um 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 tick 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 tech tic talk toc okay so that took point zero zero five six six eight seconds okay 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 all together 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 tick and a talk around that i get 0.002896 seconds so it's significantly faster than using a for loop okay 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 1 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 is just 1 to 10. and we want to say y is initialized to a an array of zeros and we'll do 4 i equals 1 to 10 if mod x of i and 2 is equal to 0 then x is even right so that means we're going to set y of i to 1. else y of i is 0. so we don't really need that else since y of i is already zero but i'm including it so you know what an else looks like okay so now if we look at y it has the ones where we expect it which is where x is even okay 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 three so let's just use the same x as before let's reinitialize y to all zeros or sorry we don't even need to do that let's set some sum equals to zero and so now i'm going to loop through all x's again four i equals one to ten so if mod x i so now we're going to say 3 right so if it's divisible by 3 we want to sum that element okay sum sum plus x of i and now this time we're not going to include the else okay so s is 18 which is equal to 3 plus 6 plus 9 which we expect so that's the correct answer now of course there's 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 of i we can just do this for little x equals big x right because the little x is going to just go through every element in the big x we're going to start again so we're going to reinitialize s to 0 say 4 x little x equals big x if mod little x three equals zero sum equals sum plus little x 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 eight so i can use a while loop for that right so i can say i can set a variable called found equal zero meaning false and then i can say while not found so i'm going to initialize and index x 4x i'm going to say while not found i equals i plus one if x of i is equal to eight which is the thing i'm looking for display i found it okay 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 of i equals eight so i'm going to reset this code i equals i plus one if x of i equals equals eight display i found it set found to true and now end the if statement right so now we go up to 8 print out 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'll 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 of i is equal to 8 display 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 eight and then we would quit versus this for loop goes from one to ten 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 four i equals one to ten f so again if x of i equals equals eight display i found it break end end so now you see i only goes up to eight it finds eight it breaks and then it doesn't go to nine and ten hey guys so in this matlab tutorial we're going to be talking about uh different kinds of data types so things that are not what we've seen so far i.e numbers scalars vectors and matrices so one thing that we've been implicitly looking at are strings so whenever i display something like this and i put it into single quotes the thing that is inside it is a string um later on when we look at opening files we'll specify the file names as strings so a string is basically any any sequence of characters so and so we've seen the workspace but there's also a function called whose that lets us look at all the variables in the workspace and see some stuff about it so you'll notice a column called class so so far everything i've created is a double so we have no string no actual strings yet if i use the class function and i look at an actual string let's just use the i founded string again you'll see that it returns car so if i set a variable called my string equals i found it and i type whose you're going to see now that there's a variable with the name my string size 1 by 11 so strings are considered row vectors and it's of type car so the next topic we're going to talk about is structs we've seen so far in matlab numbers matrices vectors arrays strings but that doesn't leave a lot of flexibility compared to other programming languages so if you're familiar with python maybe you've seen the dictionary data type or if you've used java it would be the hashmap or if you've coded in javascript you may have used json if you've ever used any of those before you know that those data types are very flexible because a json can store other jsons it can store arrays whereas in matlab we we've so far been constricted to only numbers so in matlab when you want to declare a struct it's very easy so call our struct my struct dot name equals my news correct okay so this creates a struct with one field which is referred to as name and the value is my new struct if i do class on my struct it tells me that it's a struct so the cool thing is you can add new values to the struct dynamically so i can say mystruck dot age equals 25 so now my script contains two fields name and age and notice that they are different types so if i do class my struct name it's a car but if i do class my struct age it's a double so there are also some useful functions that act on structs by struct so is field will tell you if a field exists within a struct so i just checked if the field name was part of my struct and a return one because it is let's try gender so that returns false because gender is not a field in my struct there's a method called rm field so if i want to remove a field from a struct i spelled it wrong okay so now my struct only contains the field name because i removed the aid field there's a function called set field so you might want to use this if for example the name or the value is set dynamically while the program is running so you can't do my struck dot something you have to set the field some other way and i spelled field wrong again so now you can see i've added a field called gender with the value f so the other thing i talked about is that a struct can actually contain a struct so suppose i want to add a new field to my struct called contact and i want to give a phone number to my contact two three four five six seven eight nine zero so i've set the phone number on this truck and so you can see when the struct prints out now the contact field is just a struct but it doesn't print what's inside and so now since contact is a struct i can set other fields as well okay so my struct now contains the contact field as a struct and contact is also a struct so the last thing about structs that i want to mention is an alternative way to initialize a struct so so far we've been manually entering each struct value one at a time but you could also do something like this name bob email bob gmail.com right and so what that does is it takes in an arbitrarily long list of parameters where the first parameter is a field a second parameter is a value the third parameter is a field the fourth parameter is a value and so on so the last data type that i want to talk about today is the cell so the cell is very similar to a struct in that it contains arbitrary fields um the difference is you you might see a cell as more of like an array that can hold indices other than just numbers versus you might see a struct as more of an object that contains information about some some type of thing and then a cell might be you know like an array of those things so the cell syntax is very similar to the matlab matrix and array syntax okay so to initialize our cell we're going to use curly braces we want to set the field 1 doesn't have to be an integer i'm going to set it to the value hello world okay so now if i print out my cell simply contains hello world okay so now i'm going to set another field i'm just going to call it a and it's going to be a matrix okay so now i have two keys and two values for myself i have a key one and the key a and then the values hello world and then this two by two matrix so what i can usually do with matlab variables is just type it into the command window and see what it prints out you can see that it has printed out a lot of stuff so you generally won't want to do that but notice how now i can access key one and it returns hello world and i can access the key a and it returns the two by two matrix that i previously stored so this is good for when you want to store things by name for example instead of trying to put it into an array and you can also store heterogeneous information so different types so today we're going to learn about how to make plots in matlab so one of the very simplest plots you can make is just to plot a series of data so one very popular series is the fibonacci sequence and that's just adding the two previous numbers to make the current number right so here are the first eight values of fibonacci and so to plot a sequence all i need to do is call a function called plot and pass in the variable and so that's fibonacci now notice how the x values have been set automatically so it starts at one and increases sequentially so the next thing we can do to make it a little more complex is to have actual values for x so suppose i want x to increase by 0.1 every time so i might do zero zero point one zero point two point three four five point six point seven all right so now i have an x and y which is what you're probably more used to when you're creating a two-dimensional plot and to plot x and y you just pass in x and then y into the plot function right and so now you can see the y axis is the same as before but the x axis is now no longer one to eight it's zero to zero point seven notice that the size of both x and y have to be equal all right so if i do this this is not going to work right all right so the next thing we're going to do is something a little bit more complex we're going to plot a function so the first thing we need to learn about is a function called linspace linspace what that does is it creates the range of values of x for us so we just pass in where we want it to start okay where we want it to end and how many points in between so i'm saying x is going to go from 0 to 100 and has 200 points in between now to calculate y all i have to do is sine x we're plotting the sine function so in matlab uh functions generally apply element wise right so every value of x you call sine x and then it assigns the value of that sine x into y so i'm going to plot x and y again and so now i see sine x going from zero to a hundred one thing you might notice about this is the lines are pretty jagged right it's not a smooth graph but you know that sine x is actually a smooth graph so the reason why that is if we look at x again let's look at the values remember there are 200 points for about a range of 100 and so that's a very low resolution the last value here is 100 the second last value is about 99.5 so there's only about 0.5 distance for every unit of x so to give us a better granularity we might want to say you might want to reduce the range first of all and then put more points in between so let's recreate x let's say it's going to go from 0 to 2 pi and have 100 points in between right so y is going to be exactly the same we're going to plot x y again right and so now you can see that it's a lot more smooth so adding to the complexity of this a little bit suppose i want to plot two functions on the same plot and that might be useful if you want to compare two different series so let's say the second function we multiplied is cosine of x say y two equals cos x um and so if we type in just plot we can see some different function signatures for plot if you look at the third one it shows us x one y one dot dot x and y n so what that's telling us is that for every pair x and y that we want to plot we just pass them in adjacent to each other so in our case we're going to have x and y and since it's the same x for both y and y 2 we're going to just pass in x again say y 2 over there all right and so now you can see both sine x which is the blue line and cosex which is the green line on the same plot and matlab automatically changes the colors for us so we can differentiate between them one thing you can do if you want to make it a little more fancy is you can specify a line style and so those are also passed in adjacent to the x and y's so now it's even easier to differentiate between the two lines so the second chart cos x is a bunch of dots and i made a mistake because the dash is the default that just makes a regular line if you put in two dashes it makes them into dashes that you can differentiate between like that okay so now let's say i want to save this plot to my hard drive or maybe put it on the internet or a document so you go to file save as so you see the the default file format here is dot fig and that is a matlab file so only matlab can read those files there are some other types of formats here that we can look at such as pdf if you wanna save it to a pdf and then print it out later you can save a png or a jpeg so if you want to put it in a word document or you're writing a paper or you want to put it on your blog post you might want to use jpeg or png so i'm going to use dot fig since you probably already know what a png and a jpeg look like alright so i already made a test.fig i overwrote it so now that i have it so you can see right here saved into my current working directory if i want to open an existing figure i do that through a function called open fig i pass in the file name and it opens back up the figure and so the nice thing about matlab figures is that i can still zoom in right i wouldn't be able to do that really with a jpeg it would look very blurry um of course i can zoom out i can use the hand to move around the chart right so it opens back up the same figure i had before so i can play with it as if i had made it for the first time okay guys so in this tutorial we're going to be talking about different types of plots that could be useful for data analysis so the first type of plot if you've ever used excel or watched a powerpoint presentation you've probably seen this you've probably seen most of these actually so the bar plot so suppose we have some sequence of data let's just say x equals 1 to ten and we want to plot this on a bar chart we just call the method bar pass in x and we see the bar chart so this does pretty much the exact same thing as the plot function except instead of connecting them all in a line it connects well it doesn't connect the points at all it gives them each their own individual bar so just for reference let's do um let's do our sign example again okay so we're going to do bar bar xy right so this is a bar chart of the sine function okay so the next type of plot that i want to talk about is the histogram so if you've ever seen if you ever studied probability you know about probability distributions and given a data set we might want to figure out what kind of distribution does it follow so let's generate some data where we know the distribution so i'm going to create a thousand points that are normally distributed so if i plot this it doesn't do much it just looks like noise so i won't be able to see much from that what you want to do is you want to call a function called hist which will automatically create a histogram out of your data so i'm going to call the simplest version which is hist of just the data right and so you see that it's pretty much normally distributed um there's a little bit more weight on the right bar than the left bar in the center and so the the resolution for this chart is not that great so what you can do is you can set the number of bins right so if you have a lot of data in our case a thousand points you can set the bins to maybe say 50. okay and so this gives us a much more granular representation of the data so now we can see it still looks normal um you know there's a little less data than expected kind of right here at zero and then a little more to the right of that but generally speaking this plot is pretty much what we'd expect the next type of plot that i want to talk about again if you've used excel you've probably seen this or powerpoint is a pie chart so we're going to work with some simple data again let's say x is one to five and all i do is i call a function called pi pass in the data all right so it automatically splits up the data so the dark blue is the first element light blue is the second element green is the third element and so on and so there are ways that you can label this to um to make your visualization better and so when you show it to people they know what everything stands for so there's a lot of documentation on the matlab website for that the last type of plot that i want to talk about probably the most useful for data analysis is called the scatter plot so we're going to again return to our sine example so x is lin space from 0 to 2 pi let's give it a thousand points so now i'm going to do something a little different i'm going to set it y equals 10 times sine x so that the sine x has an amplitude of 10 and then i'm going to add some random noise with variance 1 centered at zero and so the reason why i want to scale it by 10 is so that the sine wave is big compared to the noise otherwise the noise will just take over the shape of the sine wave i did that wrong okay so now if i were to just plot x and y so you can see a noisy sine wave which is okay if i do it in a scatter plot i can see that pattern pretty much just as well so the nice thing about our synax example was that the data was ordered now suppose that my data is not ordered so instead of having lin space my x is just a bunch of random points so x is normally distributed with uh standard deviation of two okay and so y is going to be let's say five times sine x plus some noise okay so if i plot x and y now i don't really see anything it just looks like a bunch of craziness but scattering so a scatter plot that will just plot all the dots so all the individual data points right and so i can now very clearly see the sine wave that i'm supposed to see so if you're doing data analysis your points are not ordered you want to use a scatter plot hey guys so in this tutorial i'm going to talk about loading and saving data so a lot of the time when you're doing data analysis your data is going to be passed around in files that you might download off the internet or just store it on your hard drive or something like that so you're not going to be manually typing in matrices into matlab all the time the first thing i want to talk about since it's probably going to be really popular with windows users is excel so excel uses a format called dot x lsx and there are two functions in matlab that are useful for reading and writing xls files okay so if you type in help xls read you can see the documentation for the xls read function there's also a function called xls write that you may want to look into so the reason i'm not going to cover xls read and write in this tutorial is because first of all it's not perfect so i saved i don't i'm on a mac so i used google sheets to export and xls document and it's a very simple document it's just a three by three matrix with the numbers one two three four five six seven eight nine in matlab so xls read matlab example gives me an error when i'm trying to read this file so we're not going to be working with xls in addition to that so if you've ever participated in a kaggle contest or you're you get a file to work with from school i've never seen anyone ever use xls usually it's a csv sometimes it's a dot matte file which is a matlab file so we're going to be working with those so csv i would say is the most common it stands for comma separated values so your file will look something like this one two three four five six seven eight nine okay i'm going to save this csv example dot csv so we have a function called csv read csv example okay and so that did exactly what i wanted it to do so you can see how the csv file pretty much one to one corresponds with the matrix that you would get after you read the flap now let's say i have a new matrix let's create something a little different okay so just fives along the diagonal calls csv right be another dot csv so i did it backwards because i didn't know so i'm going to write the matrix b to a file called another.csv so if i look at this file all right so when i double click it matlab gives me kind of a csv editor so now what i've done is i've opened the same file in a text editor on the command line console and so you can see again what we'd pretty much expect a csv of the matrix that we made so that's the nice thing about csvs is they don't just work with matlab they work with many other types of programs it's just a plain text file so now the other way that we can read and write files is the dot map file which is matlab specific so i'm going to clear my workspace and you'll see why i'm going to create one matrix a1234 oops i'm going to create another matrix 5 6 7 8. okay so now i have two matrices a and b so now what matlab lets you do is it allows you to save your entire workspace all into one file and so all i do is i call save my workspace dot matt okay so now if i look at this file so i open this dot map file in a text editor notice how it just shows me a bunch of garbage so this is binary data specifically for matlab so that's why you might prefer to use csv so you can be cross platform or cross program okay so i'm going to clear my workspace again and i'm going to call load my workspace.map and so what load does is it just reloads your entire workspace that you saved into the nat file and so that could be useful if you don't want to save every variable individually into their own csv what you can do is you can if you don't want to save all the variables you can pick specific ones so let's say i create another matrix c 9 10 11 12. okay so i have variables a b and c but i only want to save a and c so i'm going to give it a file name ac.nat i'm going to pass in so you can't pass in the variable you have to pass in the name of the variable so i'm going to pass in a and c i'm going to save it i'm going to clear my workspace and so now when i load ac.mat it just gives me back a and c only but not b 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 plots 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 sine x then i have another function which is just sine x plus some noise okay so i can plot these individually or i can plot them together or i might want to stack them up or something to see see if there's 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 okay so the 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 call subplot211 and i say plot xy okay so now my figure has x y in the first row and first column okay so now i do subplot two one two okay because i still have two rows in one column but now i want to plot something in the second slot and here i'm going to plot x and z so now if i look at my plot right so 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 suppose we have some two-dimensional data set i'm just going to make it random noise okay so there's we look at the matrix z we see that it's a hundred by one hundred so the 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 and so there are a couple ways you may have seen already from your math courses so we can we can plot an actual 3d plot so sort of how you 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 0 to 100 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 is 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 um 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 you know the red is the really high values and the blue is 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 plane visualizations so let's go in plot the sine function again all right so i want to give this plot a title all i do is use the title function a plot of sine x okay so now if i look at my plot you can see it now has the title a plot of sine x now i want to label the x-axis i'm going to label it with a very unoriginal label okay so now you can see the x-axis is labeled and you can do a similar thing for the y label as well okay so that's pretty much it for labeling suppose i want to label a subplot okay so now i'm going new create another function i'm going to subplot 2 1 1 plot x y title sign x okay so once i do that the first plot first subplot gets the title sign x now i call subplot two one two i plot x and 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 okay so i call title again and now my second plot has the title cosex my first plot has the title sign x hey guys in this tutorial we're going to talk about sound processing in matlab for this tutorial there are no toolboxes needed so the first thing we're going to talk about is what sound is so sound as you may have heard is a wave and it's a wave of air particles and so when you think of a wave usually we think of something like this like a sine wave we call that a transverse wave when we think of sound though it's a variation in pressure right that bounces off your eardrums so a wave like this doesn't really make a lot of sense there's another kind of wave called a longitudinal wave and this is the kind of wave that includes sound so if we look at this visualization of a spring this describes what is happening to air as sound travels through it now interestingly enough when you record sound on a microphone you can convert it into an electrical signal that signal can be viewed as a wave like this so we again get a transverse wave and so when we look at sound in matlab we're going to be looking at a transverse wave even though in reality it's a longitudinal wave so one thing you have to remember is that an electrical signal is an analog wave and when we represent things in matrices on computers those are digital signals so for example you could have a sound recording of say two seconds long and then that translates into a matrix of length 1000 so that would mean you have 500 samples per second and so this process of sampling is what we call discretization and so sampling is the discretization of data points along the x-axis or in this case time the other kind of discretization we have is along the y-axis so this is because numbers in matlab numbers in any programming language can only take on certain values so you can't have an infinitely precise number this is because numbers are stored in quantities of say 32-bit or 64-bit and so we have to not only discretize the signal in time along the x-axis but we also have to quantize the signal on the y-axis and so both of these lead to some error but in general say you're you're playing a wav file or you're playing an mp3 you don't really notice these differences they're imperceptible to the human ear so now that we've covered a little bit of the theory behind what sound is and how it's translated into a digital signal that we can read in matlab we're going to go ahead and open a sound file in matlab but before i do that i'm going to show you how i created the sound file so there's a free program on all major platforms linux windows and mac called audacity it's free and open source you can download it just by searching on google and so if you have a microphone connected to your computer you can record things so i've recorded myself saying hello world hello world and i've saved it to a wav file i have this wav file in my workspace and so there used to be a function in matlab called wave read that you could use to extract the data from a wav file now notice i get a warning when i use wave read when you search on google for how to open a sound file this is the thing that's going to come up so you're going to get this warning and wonder what's going on so wave read is going to be deprecated and there is a method called audio read that's going to be used instead but this is currently not showing up in google results so let's look at wave read first so it only reads wave files and so if you don't know a wav file is just raw digital sound data so we we sample the sound and we quantize it and then we save each of those data points as a as an array essentially into the file so there are different method signatures that we can use with wave read we can get just the data which i've done or we can get the data and the sampling rate so commonly we use fs to refer to the sampling rate the f stands for frequency s stands for sampling so the sampling frequency now there are some interesting things we can do once we have the data so i can plot the data sure where it went there we go so you can see here it pretty much looks the same as what i had in audacity also check out the size of the matrix so there are eighty four thousand four hundred and eighty samples just for that maybe one second of data and notice that the other dimension here is two if you think about why that is let's go back to audacity there are two channels so i was recording in stereo right so there's the left side and the right side so that's why there are two dimensions to this matrix now another interesting thing you can do is play sound so i can actually play this file back but for that i need to so there's a method called sound in matlab that you can use to play sound but we need to use a different method signature for wave read in particular we need to get the sampling rate so i'm going to use wave read hello world and return both the data and the sampling rate so i do sound pass in the data and pass in the sampling rate hello world and it plays back the sound so now you might be wondering what will happen if i try to read an mp3 file so i'm going to save this as an mp3 the exact same file so keep in mind that wave read of course only works with wave files if we want to read mp3s or any other kind of compressed sound file you should use audio read the new function for reading audio in matlab which can also read waves by the way okay so now let's say i want to play this hello world and so it sounds exactly the same so in this matlab video we're going to do some exercises related to sound processing so the first exercise we're going to do is try and reverse the audio file please pause this video and see if you can figure out how to do it yourself first okay so if you search on google how to reverse a signal you see two functions that pop up there's flip lr and flip ud so now if we look at the sound file that we opened earlier hello world from the last lecture we see that it's about 85 thousand by two so this tells us that the samples are along the rows and the each column is the left and right channel therefore we want to use flip ud not flip lr because if you flip lr it's just going to reverse the two channels but if you flip ud it's going to reverse the signal so let's do that so you see the shape is the same if i plot d2 it's not apparently obvious but this is the reverse of the signal that we had before so now i want you to play this signal okay so i want to play this signal for you guys but i have to save it first so that i can put it into the video editor that i'm using i'm going to skip ahead a little bit and talk about the function audio right so this is analogous to audio read you pass in the data and then you pass in the sampling rate okay so matlab does not support mp3 in audio write for some reason even though you can read mp3s let's try mp4 okay so now i'm going to read this file right i'm gonna get the sampling rate also i'm gonna play this back so that was hello world backwards so the next thing i want to do is i want to speed up the sound wave so it plays twice as fast i'm going to give you guys a minute to think about that and so please pause this video and then come back once you think you figured it out okay so the process of speeding up a sound wave is called down sampling so we're going to use a integer number like two so that it's a little bit easier so conceptually what you want to do is you basically want to drop every other sample of the original signal there are some other algorithms and functions you could apply that theoretically might give you a better sound but just to take every other sample is the simplest way and so if you searched for this earlier there is a method called downsample in matlab okay and so notice that if we check the size of d4 the signal is half the length it was before because we took every other sample now i want you to play this back with the original sampling rate and so now it sounds like a chipmunk because it's going twice as fast and the pitch has increased so if you think about a sine wave that doubles its frequency that's the exact same thing that's happening here and now the next exercise i want you to do is theoretically maybe a little bit more complicated but i think you guys can get it so now that i have this down-sampled version of hello world that's playing every other sample of the original how do i get it to play at the original speed so that it's not high pitched i'm going to give you guys a minute to think about that so please pause this video and then come back once you think you figured it out okay so the solution to this what we want to do is we want to play at half the sampling rate hello world and so this sounds pretty much like the original you probably can't tell that there's some information loss this has to do with a thing called compression so if you ever study multimedia and you look into this field further you will learn more about that okay guys so in this tutorial we're going to go a little bit deeper and more general than just sound we're going to talk about signal processing so one of the most important concepts in signal processing is the fourier transform if you've never heard of this you can go to wolfram.com and look up fourier transform so if you've never taken calculus or some sort of advanced university math this probably won't make a lot of sense to you but if that were the case you probably wouldn't be doing this course in the first place the fourier transform is essentially in a in more of a conceptual way it lets you view the frequency components of a signal so you convert a signal from the time domain into the frequency domain and then you can use the reverse fourier transform to go from the frequency domain back to the time domain so now if you look at these equations you'll notice that f of x the original signal is a continuous function of x and so we call this the continuous fourier transform now since we're working in matlab and we're working with arrays and matrices that won't really work for us so we need a sort of discrete version of the fourier transform and appropriately it's called the discrete fourier transform so let's look that up okay so we've sampled f of x or sometimes we call it f of t since you know if we're looking at a sound signal it's varying in time so we sample f of t at a certain frequency delta so that's the sampling period and then we assign those values at each sample to f of k and so f of k becomes a discrete signal one thing to note from last time is that we talked about sampling and quantization so this is sampling only not quantization so once we have f of k we can then calculate the discrete fourier transform f of n so this brings us to the next concept is what's the function in matlab that actually calculates the discrete fourier transform so there's an algorithm called the fast fourier transform also known as fft for short it calculates the discrete fourier transform in n log n time so if you were to if you ever studied algorithms you know about o of n big o notation so if you were to calculate the fourier transform naively you would get an o of n squared algorithm which is slower than n log n we don't need to worry about the details of the algorithm just that matlab has a function called fft so as an example let's do sine wave so let's calculate the fft of sine wave so now let's try to plot the fft or the transformed signal and so you'll notice it looks kind of weird this is not what we want to see so this is just a an intermediate step that i'm showing you so if we go back to the definition the fourier transform so let's look at the discrete fourier transform you'll see that it's the original signal f of k times e to the power of negative two pi i n k over big n so the exponent to the power of i where i squared is negative one is going to give you a complex number so there's going to be a real part and an imaginary part you can use euler's equation to see this more easily cos theta plus i times sine theta so there's a real part and an imaginary part so if we look back at big y which is the transformed signal you'll see that each of the parts here has a real part and an imaginary part so typically when we're plotting the fft we usually just look at the real part so there's a method called real in matlab so suppose i have some number two plus three times i really will just drop the imaginary part so now i'm going to plot real times big y which is the f of t i have little y okay and you can see that there are two spikes one on the left and one on the right so when you're looking at the frequency domain the fourier transform you really only need to pay attention to the left half so this area is the lowest frequency this area in the middle is the highest frequency and because if you look at the fourier transform again there's this two pi in the exponent and also that it's a complex number which then resolves to cosines and sines the fourier transform is actually periodic so it's periodic in two pi and so when we look at the continuous fourier transform you'll see that the signal actually repeats every two pi so when you look at a signal so let's just look at see if we can find one okay this is a good picture what i'm trying to explain so usually the way that the fourier transform is visualized is that it's symmetric around the center so we only show from negative pi to pi in the frequency domain and then anything you see on the left side of the zero point here it's just carried over to the far right when you're looking at the discrete fourier transform another thing to notice is that the transform signal is the same size as the original so size big y is the same the size little y so the important thing i want you to grab from this is that the sine wave has only one frequency right and so that's what we're seeing here that's what this spike is now so when we look at real signals so we're going to look at some voices soon what we're going to see is there are going to be multiple frequency components right so when you speak or when you play an instrument they produce different sounds because they have different frequencies playing at the same time so we're going to go back to the hello world from the first and second lecture equals audio read world three all right so remember that d has two channels so we're just gonna look at the left or first channel in the fft lectures so i'll say big d is equal to f of t of little d so i'm going to select all the rows and then the first column now i'm going to plot the real part of the fft of d right so you can see the multiple frequency components that show up this is my voice so i've downloaded a female version of hello i couldn't find hello world so she just says hello i'm going to read that in so i'm going to play it just so you know what it sounds like to do this hello okay so the female voice is more high pitched than the male voicing so mine is a male voice so what we should see then i'm going to take the fft of the female voice and i'm going to plot that so this doesn't tell us much because it's just a plot by itself so we're going to use some of the things we learned before okay i'm going to use subplot i could try to plot these on the same plot but we have a problem right so if we look at the size of the it's about 85k and we if we look at the size of f it's only eight thousand so it's a much shorter uh hello so the problem with that is if we plot them both on the same axis one's going to be really short and the other one is going to be really long so you can't really compare the frequencies that well so what we're going to do is we're going to use subplot i'm going to plot the female version up on top and i'm gonna plot the male version which is me at the bottom okay so remember that the signal is symmetric right so i only have to pay attention to one side and they're relatively the same length on these plots so now notice that the female voice takes up higher frequencies and my male voice takes up lower frequencies and its loudest on a very low frequency so the fourier transform can be used to analyze sound signals or any kind of signal that you want to know the frequencies of okay guys so in this matlab tutorial we're going to extend what we learned last time about the fourier transform so last time we took the fourier transform of a sine wave and if you've ever studied the fourier transform in school you know that when you take the fourier transform of a sine wave you should see a delta function or a spike in the frequency domain and what we were seeing was something a little strange so let's suppose i set my x to linspace zero going uh five pi with a thousand points and then i s plot real fft sine of x okay so what we see here is actually two spikes not one spike um and this is due to artifacts due to sampling we don't have an infinite sine wave which is assumed when we calculate the fft of the sine wave and then we get a delta function in the frequency domain so what we could do right if you want to have closer to an infinite sine wave you could do 0 to 10 pi so that's 5 full sine waves and then do 500 samples let's plot sine of x again and so now you can see something much nicer so now i want to extend the idea of before we transform a little bit so let's say we're doing the fourier transform of a song or somebody playing the piano the frequencies if you've ever studied music you know are at certain notes are multiples of each other and so the frequency is going to change as the song progresses now how do we show that using the fourier transform the fourier transform takes the entire signal from time equals minus infinity to infinity and so what we're really doing is we're taking all the different frequencies over all of time and then plotting that on the same fourier transform and so the answer to that question is we use something called the short time fourier transform and so the way that works is it's a two-dimensional heat map or you can think of it as a contour plot or something similar that shows time on one axis and frequency on the other axis it shows you different frequency components at different times and the way it works is it takes the fourier transform of small windows of time and then moves it along the signal as time goes on so it's only showing you the frequency spectrum of a small window at each time another name for the short time fourier transform is the spectrogram so if we look here there are different function signatures that you could use notice that we require the signal processing toolbox if we want to use this method so spectrogram looks something like this you have time and frequency and it shows you how the frequency the main frequency component is changing as time goes on so how do we create let's say we want to test this function out how do we create a function that changes frequency as time goes on so let's say we set x as before actually i've already assigned it so it goes from 0 to 5 pi or 0 to 10 pi and has 5 000 points so suppose we set the frequency so a wave looks like this frequency times time so suppose we set the frequency omega and i'm just typing this out this isn't a real matlab frequency as a function of time is just equal to the time so we're going to increase the frequency linearly as time goes on so if we combine those two we would get something like this y2 equals sine x dot times x because this is element by element multiplication okay so now i do the spectrogram of y2 and you see that the frequency is increasing linearly so it takes an equal size step at each window and notice that there are some artifacts due to the fact that the frequency is not equal within the window that we're calculating the fft so you notice that it looked kind of blockish what you can do is you can pass in the window size along with the data so let's try a window size of 100 so you now you can see it looks much more like a line so let's try that again with a window size of 500. so now you see the the red line is a little bit skinnier but the the time windows are a little bit bigger so this is what we call the uncertainty principle and you may have heard this if you've taken quantum mechanics the uncertainty principle when you measure the momentum and it's very accurate you can't measure the position as accurately so it's the same when you're taking the fourier transform because if we want to know the frequency very accurately we need to have a very long window of time but that gives us different frequencies at a lot of different times if we want to know the frequency at a very specific time we have to shorten the window but then we have we have a frequency that's not as accurate okay guys in this matlab tutorial we are going to talk about low pass filters and how to apply those to an audio stream so to first recap and kind of give you a different perspective on what we've been doing we're just going to listen to a sound wave at different sampling frequencies so i'm going to create my x-axis zero going 50 pi with 10 000 samples i'm going to say y equals sine x so this sound is only at one frequency so you're going it's going to sound like kind of like a buzz so i'm going to play this sound at a common frequency that's used for recording mp3s and waves 44 100. so let's listen to this [Music] okay and so now what i'm going to do is i'm going to play the same sound wave but at a lower frequency or sorry a lower sampling rate and that should give me what sounds like a lower frequency even though it's the same wave it's the same matrix of values and so it indeed sounds lower pitched and so now what we're going to do is we're going to create a low pass filter so what that does is say we have multiple different sine waves playing which is essentially what the fourier transform is giving us we're going to filter the signal so that only the low frequency components can be heard so if we look at the typical shape of a low pass filter you'll generally see things like this and remember that this is only the left half of the plots that we've been looking at when we're in the frequency domain convolution turns into multiplication so right here zero decibels is actually equal to one because it's a it's a log scale so so we're multiplying one by the signal at these low frequencies but then as we go down here we're multiplying a very small number by those higher frequencies and so after we've multiplied one signal by the other one of them being this low pass filter those high frequencies are going to be silenced so if you've never studied about low pass filters before you may want to go and look up the different kinds what's their function representation in the time domain and the frequency domain we're going to use a very simple version of the low pass filter called the moving average so matlab already has a function for us that can create filters we just need to pass in the b and the a which is essentially the different multipliers on the original signal okay so i'm going to set b to equal 1 over 40 40 times i'm going to call i'm going to load my data first dfs equals audio read hello world dot wave okay so i'm going to say dlp equals filter b so i want 1 as the numerator and then b is the denominator passing the data so now i want to play the original hello world for reference hello world and i'm gonna play the new low pass filtered version to compare hello world so you can see the or you can hear the low pass filtered version sounds very muffled sounds like you're speaking through a pillow or something and that's because high frequencies tend to not go through different medium so low frequencies can go through a wall so if you hear your neighbors talking for instance it'll sound a little muffled and that's the same thing that's happening your wall is acting like a low-pass filter so let's do another way of comparing these i'm going to plot them on the same axis it's convenient because they are the same size okay so now if i zoom in a little bit so you can see all the sudden changes in the dark green signal don't happen in the light green signal which is the low pass filtered version that's because when you see sudden changes that's actually you can think of it as a very fast sine wave that's super positioned on a low frequency signal and so we're essentially just getting rid of those now yet another way to compare these two signals is to plot them in the frequency domain so for this we want to use subplots so that they don't overlap each other so let me just do it all in one line okay so remember that the original signals on the top and the filtered version is on the bottom so now you can see that we're keeping the low frequencies but the high frequencies which seem to have some components that are non-zero are all dampened or set to zero in the filtered version now if you're interested in this what i would recommend as an exercise is to try to implement a high pass filter as a hint you're going to want to use the same function filter and everything else is going to stay the same hey guys so in this matlab tutorial we're going to switch gears a little bit to a related topic image processing the first thing i want to talk about is what is an image so we can compare it to sound waves since we've already studied those now let's think about the most raw data possible so firstly whereas sound is a one-dimensional function images are two-dimensional functions we have an x and a y and a z and so the z is the value at that x and y in fact you can think of a heat map or a contour map as basically images so we have a 2d function okay x and y are continuous coordinates in space and like with sound we have to sample the data in order to store it in a computer so with sound we're sampling across time and we call that the sampling frequency what's analogous to the sampling frequency when we're dealing with images i'll give you some time to think about that this is actually probably pretty familiar to you so with images the analogous concept would be the resolution right so the the amount of fine grainness you put into each point in space for example udemy videos are required to be recorded in hd resolution so now let's suppose we have an image called a a will have the indexes i and j referring to the x y coordinates just write this down if it helps you so we have an image a and then a i j would be the x y coordinates so now let's think about what would be stored at aij in other words how do we store a pixel so now let's suppose we store just one number at aij so it's a two dimensional matrix or a table it's a table of values at each point x y that image would be that matrix would represent a grayscale image right because if we only have one number we can only represent intensity so zero would be black and one would be white and so a i j would be the intensity at the point i j in order to store a color we need three parameters r g and b or red green and blue and this is what we've been looking at for the entirety of this video so far so these values usually vary from zero to 255 so we can have a total of 256 to the power of three possible different colors so let's look at what the value of that is so that's about 16.8 million different colors we can represent if we store values from 0 to 255 for rg and b now what this also means is that if we have a 500 by 500 image the matrix that we use to represent it isn't 500 by 500 but it's 500 by 500 by 3. sometimes this is called an image bitmap all right so now that we've talked about images in the abstract sense let's talk about how you would actually open an image in matlab so there's an image processing toolbox that we won't be using a lot of in these tutorials matlab has a function called i am read built in we are going to use that function to read in a very famous picture often used in image processing so if you ever take a computer vision course or a multimedia course you've probably seen this image before okay so let's look at the size of a so it's 512 by 5 12 by 3 so a three dimensional matrix as promised notice the data type is uint 8 instead of double that i have for my other matrices which is the uh default in matlab so a uint 8 the u stands for unsigned n stands for integer and 8 means it's 8 bit okay so we think about how many values could be stored in a unsigned 8-bit integer let's go ahead and calculate that you should pause this video and try to think about the answer yourself first okay so the answer is 2 to the power of 8 and that's 256. so as we discussed previously the values for rg and b go from 0 to 255 which is 256 different values there is a function called i am show that will essentially plot the matrix as an image so that's the image the famous lina image that's used for image processing so in this matlab tutorial we're going to do some basic exercises to just get you working with image matrices and they're going to be very similar to the exercises we did for sound so the first exercise i want to do is how do we flip an image upside down i'm going to give you a minute to think about that so please pause this video and then come back when you have figured it out okay so if we want to flip an image it's going to be exactly the same as we did for sound so if you recall a sound was stored in using different samples in different rows and essentially that's what's happening with an image also right so the top of the image is the top of the matrix and the bottom of the image is the bottom of the matrix so if we do flip ud which stands for flip up down of the image that we loaded and then we i am show this image we see the original image flipped upside down so now here's something we couldn't do with sound because sound is one dimensional so you can only flip it one way now let's suppose i want to flip the image in the horizontal direction how would i do that so i'm going to give you a minute to think about that please pause this video and then come back when you figured it out okay so if you remember when we were talking about sound we looked at two different kinds of flip there was flip ud which flipped vertically and flip lr which flipped horizontally so all we have to do is call the other flip function i'm gonna i am show this flipped image all right and so this is the image flipped horizontally and of course you could flip the image both vertically and horizontally to get this okay so now the next exercise we're going to do involves working with color so i've said before that the third dimension in the matrix are the red green and blue channels of the color so how can we how can we show this so let's say i only want to view the blue channel how can i how can i visualize that so i'm going to give you a minute to think about this please pause the video and then come back once you've figured it out okay so usually we say rgb because red is the first component green is the second component and blue is the third component so i'm going to assign the original matrix a to a temporary variable b i want to view blue so what i want to do is i'm going to set red and green to zero so how would i do that i say colon to select all of the rows another colon to select all of the columns and then i put a one because i want to set the first channel which is red to zero i'm going to do the same for the green channel and so now only the blue channel has values that are not zero so if i i am show [Music] b now we see only the blue components of the image right so if i i am showed one of the other channels that was set to zero it's just pure black so zero is black means no intensity at all so next we're going to look at the green channel so i'm going to set g equals to a g colon colon so i'm setting again red to zero but now i don't want to set green to zero i want to set blue to zero so it's g colon colon three equals zero and i am show g and so here is the green channel so it's a little bit more intense or it has a higher intensity than the blue channel we can see visually so now let's say we want to view the red channel so we're going to do something very similar to the above so i don't want to set red to zero i want to set green to zero which is the second channel and i want to set blue to zero which is the third channel so i am show r and this is the red channel so one thing to notice is that no matter which channel we look at we can still pretty much just view the image now why is that it's because every color contains a red component a green component and a blue component so when you see colors think of them as sort of a mixture of those three hey guys so in this matlab video we're going to talk about convolution so i mentioned this before when we were talking about the low pass filter because they are very similar and related concepts so when we were talking about the low-pass filter we did a very simple filter called the moving average and so to give you a sense of what that's doing again you're taking a window say five samples at a time and then you're sliding that along the signal and taking the average and then that is the output of the filter so this sliding motion and then applying some function to that window that's sliding along is called convolution so what you're really doing when you're applying a filter is you're convoluting one function with another so let's look at the definition of convolution okay so convolution is also known as the star operator and it's the integral of one function with another with this dummy variable so you can see this sliding motion that i was talking about now this won't make a lot of sense to you if you haven't studied calculus before but there is one important result from convolution or the study of convolution that we should talk about and that you should know and that is that convolution in the time domain and we use time as a sort of dummy variable so time can mean actual time or time can mean space the important distinction is that you're going from time in one domain to frequency in the other domain so the result that's important is that convolution in the time domain so if i convolve one signal with another this is equivalent to multiplication in the frequency domain so what is the significance of this so that means there are two equivalent ways of computing the convolution of the signal with its filter so one way is to just do the convolution using the formula for convolution which is here and of course in matlab it would be a sum not an integral since we have to discretize the signals but the other way we could do is since convolution in time is equal to multiplication and frequency we could take the fourier transform of both signals first multiply them and then do the inverse fourier transform to go back to the time domain and so that would be equivalent to doing convolution one application of this is the fourier transform or the laplace transform can be used to solve differential equations so you can solve them in the frequency domain and then convert back to the time domain to get the signal of interest back so now again when we think about convolution a very useful analogy is this sliding motion and so that's exactly what the moving average was doing and so one physical manifestation of this is the blurring of an image and so you'll see in a later lecture that i'm going to do that the blurring of an image is actually convolution but the sliding motion you actually do you can do by hand and it has the same effect so this is to say if you've ever used photoshop and you've used the blur tool on an image you notice that you click on the blur tool and you get a you get a point and then you slide it across the image on the places where you want to blur and then it blurs those points hey guys and welcome back to this course on matlab and probability in this lecture we're going to talk about a special continuous distribution called the normal distribution or the gaussian distribution it probably looks very familiar to you since it is what most people refer to as the bell curve and you've probably seen this in school where bell curves are used to shift marks up or down based on how well students perform so this formula you see here is the pdf of the gaussian distribution notice how they also use the little f notation on wikipedia the interesting thing about the gaussian distribution so we talked about last time that the mean and the variance are two special numbers that help us describe what a continuous distribution looks like with the gaussian distribution the mean and the variance completely describe the shape of the distribution so the mean tells us where the center peak is of the bell curve and the variance tells us how much that bell curve is spread out so you can see this yellow curve is very spread out and the blue curve is spread out not that much so let's talk a little bit about this formula first there is a normalizing constant it's 1 over the square root of 2 pi times sigma which is the variance actually sigma stands for the standard deviation usually it's written as 1 over the square root of 2 pi sigma squared where sigma squared goes inside the square root so sigma squared is the variance and sigma is a standard deviation second part of the pdf is this exponential so we take the negative of x minus the mean which we denote by mu square that divided by 2 sigma squared or 2 times the variance and then we exponentiate that note that since we square the thing where x is this pdf is symmetric so if you go a distance from mean to the left or that same distance to the right you will get the same value for the pdf so let's do an exercise where we plot the values of a gaussian curve from say minus 100 to 100. so we'll create a new function call it my gaussian we'll take in two parameters mu and sigma squared and we'll output an array so n will be the number of different values between min x and max x so we're going to start our little x value at min x and then we want to know how much to increment x on each iteration of the loop so we'll call that dx and we'll say it's max x minus min x divided by n so at the end of the loop we're going to add dx to x we're going to call this f okay so we're going to return uh let's return the array of x values also so x y is going to equal 2x and f of i is going to equal 2. so return x and f so 1 over the square root of 2 pi sigma squared times exponential of negative x minus the mean squared divided by two times sigma squared so let's do this for mu equals zero sigma squared equals one say minus let's say minus ten to ten and then have a thousand values between them okay so now we can plot x and f all right so we get this bell curve so the peak is at zero because that's the mean and then it's spread out and from about negative two to two so the drop off or how fast f of x goes to zero is pretty quick you can see the maximum value is about zero point four let's try that again with a smaller variance and some smaller values also of min x and max x so let's do 0.1 for the sigma squared let's plot it again all right so the drop off is even quicker now where we get to about zero at minus one and 1. and notice the peak value is above 1.2 so since this is a pdf values above 1 are allowed okay so now we're going to move on to the next step in order to implement our blurring tool or our blurring filter so the first thing we need to do is we need to extend this idea of convolution to two dimensions so the first modification we did so we looked at convolution at its most basic definition where it's of a continuous variable filtered by another continuous variable so we discretize it by turning it into a sum and so we already have discrete signals because we're working in matlab now when we do two-dimensional convolution notice we now have two dummy variables so before it was tau or k now we have two dummy variables tau 1 and tau 2 or n1 and n2 and so it's basically what you would expect when you have 1d convolution you're going from minus infinity to infinity along the one dimension that's the independent variable so you can think of that as time and in two dimensions we go from minus infinity to infinity for both independent variables so that would be x and y or in other words the two spatial dimensions of the image okay so now that we've extended our idea of convolution to two dimensions let's think about how we could implement a blur using a gaussian so a gaussian is basically a spread right there's a there's a middle point and then it spreads out over a radius in a circular fashion so my question to you is how can we build a two-dimensional gaussian image that we can use as a filter on the original image so that we can do a convolution between those two so i'm going to give you a minute to think about that please pause this video and come back when you've figured it out okay so i have in fact actually been showing you the solution to this for multiple videos now so i've created a function called my gaussian and it takes in two parameters n and sigma so n is going to be the size of the square so the output is going to be an n by n matrix and sigma is going to represent the standard deviation of the gaussian as is convention when we're talking about gaussians so we have the output called h now i only need the value of two sigma squared ever in this equation so i'm just going to calculate 2 sigma squared at the beginning so i don't have to do that on every iteration of the loop i'm going to initialize h to be an n by n matrix of all zeros and then i'm going to use a for loop to assign every value of h so i going from one to n and j going from one to n now i assign x to equal i minus n over 2 and y to equal j minus n over 2. why is that because i want the center point of h so that would be h of n over two n over two to be the highest point of the gaussian and so that's when x is equal to zero and y is equal to zero and the exponent of zero is one so that would be the maximum value of the gaussian and then every point from there would be smaller right so when i is n over two x is zero when j is n over two y is zero and the formula for a gaussian is x squared plus y squared over 2 sigma squared and then you take the exponent of the negative of that okay so what does this actually give us so let's use the my gaussian function say n is a hundred and sigma is 10. now let's i am show the h that i got okay so you can see so remember that white is the maximum value one and black is the minimum value zero and so this is a gaussian what a gaussian looks like when you plot it on an image so what would i do if i wanted to see more white i could increase sigma right so let's say sigma is 25 i i am show h and so you see the radius of the white part has increased so now let's take our gaussian and convolve it with the image so now remember that a is 5 12 by 5 12 by 3 which is a three dimensional matrix and h is a two dimensional matrix so if i try to do this i'm going to get an error sorry we are using the conf2 function and you still get an error because a is not supposed to be a three-dimensional matrix right so if we look at the definition of convolution we're working with two-dimensional matrices if we have two-dimensional convolution so what we want to do is let's just take the red channel right it doesn't matter which one because remember when we were looking at the red green and blue channels we pretty much saw the same image for every channel okay so we still get another warning and that's that a is still in values of u and eight so let's change that to double okay so now let's i am show c see what we get so it's all white and so what does white mean that means all the values are too high right so there's too much intensity in this image so you have to play around with the values a little bit so let's try making a smaller filter okay so let's say h is equal to my gaussian 25 and the sigma is 5. so now the filter is smaller and the sigma is smaller so the blur is going to be less but the intensity is still too high so that could mean just the values are too high and we need to multiply by lower values so let's divide the filter values h by 1000 i am show c again so now we're starting to see some black right so that's good okay so i'm decreasing the values and i'm starting to see the image okay so here is the original image but blurred using a gaussian of sigma equals five now one thing i didn't show you guys because i wanted you to go through the exercise of creating a two-dimensional gaussian filter yourself is that we already have a function called f special in matlab that creates filters for us and so f special can create many different types of filters so in addition to the gaussian it can create laplacian filters an averaging filter which is another thing we've used a sibel filter which is useful for finding edges so all different types of filters so let's try using f special instead what was f special now let's do our convolution so i'm going to divide by a thousand because i know i'm gonna have to divide show c let's let's just see what we see okay and so it's a little dark so i didn't have to divide by a thousand maybe i could have divided by a lower number but you can see the idea is that we've blurred the original image using the gaussian filter given to us by f special instead of our own gaussian filter that we built okay so like we did before i want to plot the filter that we created so i'm going to go i am show little h which is the filter we created with f special and notice how it's just all black remember that zero is black and one is white so what we could do we want to look at what's in h let's check the maximum value of h so max max because it's two dimensional so the maximum value is .0065 so of course when we plot that it's going to be pretty close to black so what we could do if we wanted to scale it by one we want the maximum value to be one is we could do i am show h divided by the maximum value okay so now we see what we expect to see which is white in the middle so one question you might have is why is the value of h so small when we use f special and that's because it does a thing called normalization so if you have ever studied probability you know that a probability distribution has to sum to one so here it's a similar thing so if we sum across both dimensions of h we get one and so that's why the values of h are so small and so now just for completion sake i want to do sort of what's the opposite of blurring i want to do edge detection of the image so i want to find all the edges and set those values to 1. so matlab has a method called edge that will do this and it's very simple to use you pass in again the grayscale image right so only a two-dimensional matrix it won't work if you pass in the entire image matrix okay so edge just does all of that automatically for you so if i i am show e i can more or less see where all the sharpest edges are and of course there are parameters you can pass into edge to make it more or less sensitive but this is by default what it does hey guys and welcome back to this class on matlab and probability in this class we're going to talk about the multivariate gaussian so in all our past lectures we've looked at the one-dimensional case so one-dimensional distributions both discrete and continuous when we talk about the multivariate gaussian distribution we're talking about two or more dimensions and of course matlab is perfect for this because it works with all matrices and vectors so now what happens when you extend the gaussian like this so the first thing is that the mean becomes a factor and again this doesn't really change the definition of the mean you still add up all the values of x and divide by n to get your sample mean so as an example let's say we have a random matrix with 100 values and with 10 dimensions right so if we do mean x we get a 1 by 10 matrix and each component is the 100 values in that column summed up together and divided by a hundred the thing that is kind of more different than uh the one-dimensional case is the variance so we see this sigma symbol here and what this stands for is the covariance matrix so with the variance told us how far x was spread out from the mean and the covariance does that too but only on the diagonals so on the off diagonals it tells us how correlated one dimension is with the other and so you can see here the definition of the covariance matrix is the covariance of the if dimension with the jth dimension and that would be the ijf element of the covariance matrix don't really need to worry about how to calculate it because we are in the end going to end up using the matlab function cove so let's check that covariance is ten by ten so our dimensionality is ten what's interesting about the pdf of the gaussian in the multi-dimensional case is that we never use the covariance directly we use the inverse right here in the exponent and the determinant and again since we already wrote a function to calculate gaussians before i'm not going to ask you to do it again there's already a function called mvn pdf at matlab that will do this for you so i've got an example here that i want to show you we set our mean and our covariance don't worry about this code too much since it is not the focus of this lecture we're simply setting the x-axis so the x1 and x2 values note that the dimensionality of x is two so mu is two dimensional and sigma is two by two so here we calculate the pdf value using mvnpdf and we're going to do a surface plot of f versus x1 and x2 so now some points worth talking about is this distribution is more spread out on the x2 axis than the x1 axis and that's because you see here this distribution has a variance of 1 on the x2 axis but a variance of 0.25 on the x1 axis and since the off diagonals are not zero what we get is a gaussian that is not perpendicular to the x1 and x2 axes hey guys welcome to this course on matlab and probability this first lecture will focus on the introduction and course outline because this course uses matlab it won't follow a traditional probability and statistics course outline rather i'll show you as we go along how the concepts of probability can be applied or viewed through the lens of matlab more generally i want to show you how a programmer might approach problems in probability so now let's talk about some of the topics we're going to go through in the second lecture we're going to talk about what is probability and we're going to give some definitions and examples in the lecture after that we're going to talk about how we can measure probability given some data so how to open a file and measure the probability of some of the features of your data in the next lecture we'll talk about how do you generate random data so how can you do a probabilistic simulation in the next lecture we'll talk about a famous problem in probability called the birthday problem or the birthday paradox in the next lecture we'll extend the idea of probability from discrete variables to continuous variables in the lecture after that we'll talk about a special continuous variable distribution called the gaussian distribution or the normal distribution in the next lecture we'll talk about if you have some data that is continuous how do you test if it is gaussian distributed lecture after that we'll talk about if you have two different gaussian distributed groups of data how can you compare the two and then in the last lecture we'll extend the idea of the gaussian to a multi-dimensional gaussian so what will you be able to do by the end of this course you'll understand mathematical problems that contain uncertainty you'll be able to quantify uncertainty both in theory and in practice you'll be able to use matlab to measure uncertainty in your data you'll be able to use matlab to simulate systems that contain uncertainty you'll be able to understand and calculate probabilities in the famous birthday paradox you'll understand the mathematics behind the very famous bell curve or normal distribution or gaussian distribution you'll know whether or not the data you're working with is gaussian distributed and you'll know how to handle gaussian distributed data i look forward to teaching you hey guys welcome to this third lecture on matlab and probability in this lecture we're going to stray from your typical probability course so we're going to have some data matrix and we're going to measure the probability of a variable that that data matrix represents so what i want you to do is i want you to go to in your web browser github.com lazy programmer slash matlab dash probability dash class once you go there you're going to copy and paste this ssh clone url then i want you to go into your terminal and type in git clone and then paste that url i've already done it so i'm not going to do it again this is going to give you some files that are relevant to this class that i'm going to use for the coming lectures okay so now that you have those files you want to go into matlab change your directory to work in that same directory that you just checked out from git okay so we're going to load this data okay so r is a 100 by 1 matrix so let's just plot r let's see what it looks like right so it has a bunch of random values that are between five and five so now let's say i wanted to calculate the probability that r is equal to minus five how would i do that so one way is i could sum all the values where r is equal to minus and divided by the total number of values in r so that gives me 0.07 i can do the same thing for every other value in the matrix so we get you know about 0.06 to 0.11 so this is what we call the frequentist view of statistics it means that if we flip a coin one thousand times and that's a fair coin with heads or tails with probability 50 we should get heads about 500 times and we should get tails about 500 times the idea is that as the number of coin flips approaches infinity our measurement of the probability of heads should approach 0.5 oh and so we can also plot the histogram of r and this should give us an idea of the shape of the distribution right so let's do a little more complex examples let's say i want to calculate the probability that r is even how can we do that so in the same way as we did before we might want to say r equals negative 4 or r equals negative 2 but that wouldn't be the best way to do this right we could use the modulo function so if r mod 2 is equal to 0 that means r is even divided by the length of r right so the probability that r is even is 0.43 and we can do the same thing to determine the probability that r is odd and so notice that these two events are disjoint you can either have r equals even or r equals odd and those two events are the only possible events so their probabilities should add up to one and we can verify that 0.43 plus 0.57 is equal to 1. now so let's say i want to test if r takes on a specific value so let's say i want to calculate the probability that r is equal to negative five or positive five how would i do that so we'd use the or operator so r equals five or r equals negative 5 divided by the length of r so the probability that r is equal to 5 or negative 5 is 0.21 notice that we can use this same method for our first problem which was to determine even or odd which is the way i suggested not doing it but we want to check our answer and so that gives us the same answer the probability that r is equal to negative 4 negative 2 0 2 or 4 is the same as the probability that r is even hey guys and welcome back to this class on matlab and probability so in this class we're going to talk about generating a random variable from a certain distribution this could be useful for doing simulations of systems that have uncertainty matlab has some built-in functions to help us do this so the first one we're going to talk about is called rand i and it takes one argument called imax and this function gives us a uniformly distributed variable between 1 and imax so let's try it right so 9 is in between 1 and 10. now there's another function rand i which takes in a maximum value and another parameter called n so let's set n to 3 so that returns an n by n matrix of random values between 1 and imax so suppose i wanted to generate random values between 10 and 20. how would i do that because rand i can give us values anywhere between 1 and imax so what we could do is we could just add 10 to all the values that randi returns so this gives us a three by three matrix with values only between 10 and 20. another useful function is just rand by itself so this function gives us a random number between zero and one so it's different from the previous one where we don't get integers we get real numbers rand returns a number with a uniform distribution so the probability of getting 0.25 is the same as the probability of getting 0.75 so let's try and plot histograms for different values of n all right so this is a histogram for random numbers between 0 and 1 4 and n equals 10 array all right so it's not quite uniformly distributed let's try a bigger n though all right so immediately it starts looking more uniformly distributed as n increases so let's try a bigger n alright so it looks even more uniformly distributed now it's ten thousand right so it's almost flat even it's a hundred thousand and this is a million it looks almost perfectly flat so that's the idea with the frequentest view of probability is that when n approaches infinity your probabilities approach their true values so now let's think about a different problem suppose i want a specific discrete distribution so say i want to simulate an unfair coin so to write it out i want p of heads equal 0.25 and i want p of tails to equal 0.75 how could i write a function to give me random values that could draw from this distribution instead of a uniform distribution so we can create a function to do this i'm going to call it bias coin it's going to take in one value little p which represents the let's call it p heads probability of getting heads and it's going to return the coin face so we're going to generate a random value if it's less than p heads we're going to return heads else we're going to return tails oh so let's try our function all right so now we're to try our new bias coin function by uh initializing an array of say size 1000 you know we're going to do this in a separate function so we're going to initialize a n by one array we're gonna count from one to n and we're gonna use the bias coin function to generate a value for each element of the array all right so let's try the function we just made this bias coin 0.25 for n equals a thousand all right so you see number of heads which resolves to i think the integer 104 and then tails resolves to the integer 116. so you see this is about 250 and this is about 750 which is what we would expect in the thousand coin tosses hey guys and welcome back to my course on matlab and probability one interesting and popular problem in probability is called the birthday problem or the birthday paradox and the problem goes something like this so given a classroom of and students what is the probability that at least one pair of students shares a birthday now you might be surprised that at n equals 23 the probability is about 50 percent which is why this problem is called a paradox so that means in an average size classroom there's probably a pretty good chance that two people in the class share a birthday this is counter intuitive since there are 365 days in a year in this lecture i'll show you the theory behind the solution and how to visualize it in matlab so the first thing is the problem of at least one pair of people sharing a birthday is difficult but remember that the probability of all distinct events have to add up to one so what is the opposite of at least one pair of people sharing a birthday it's not two people sharing a birthday or three people sharing a birthday or two pairs of two people sharing a birthday these events all fit into at least one pair sharing a birthday so the the two disjoint events that we want to talk about are the probability that at least one pair shares a birthday and nobody shares a birthday so these two events are disjoint and therefore they have to add up to one so we can calculate then the probability that two people or at least one pair of people shares birthday as one minus the probability that nobody shares a birthday and so in mathematics we would call this a counting problem so now let's think about how do we calculate the probability that nobody shares a birthday so there are 365 days in a year now if you think of each day as a bucket we have one person and they have 365 buckets to choose from the probability that this one person will collide with another is zero the probability that one person shares a birthday with somebody else when there's only that one person is zero so the probability that nobody shares a birthday in this case is one or 365 over 365. now what about two people so with one person already having chosen a birthday or a bucket the second person has a one out of 365 chance of colliding with that person so the probability of at least one pair having a common birthday is 364 over 365. so this is the case with two people now if we had three people the third person only has 363 buckets to choose from so we have 364 over 365 times 363 over 365. and we can multiply these probabilities because they're independent so this is the probability that three people and a group of three people at least one pair would share a birthday so we can continue this pattern but it would probably be easier to write a matlab function to do this so my birthday function is going to return all the probabilities up to the value n so i'm going to initialize a to b an array of zeros and i'm going to count up to n and fill in the values of a actually i'm going to say ones because we're subtracting from one actually doesn't matter what i initialize a2 because i'm going to say one minus over here okay so we know that we have to multiply by something over 365 each time and subtract that from one and so we can use a for loop to iterate over the thing that has to be subtracted and multiply the new value iteratively now that we have our function let's test it so let's say a equals birthday and let's set n equal to 100. and let's plot a so you can see here when n is about 23 you get the probability around 0.5 when n is equal to 50 you're at above 90 so there's a pretty good chance in a group of 50 people then at least one of those pairs of people shares a birthday so let's check a23 right it's about 50 and that is the solution to the birthday paradox hey guys and welcome back to this course on matlab and probability in this lecture we're going to talk about continuous variables so we've talked about discrete variables up till now discrete variables can only take on distinct values but continuous variables can take on any value so with continuous variables we don't have a notion of probabilities for exact values because x can take on an infinite number of values so the probability of equaling any specific exact value is zero we can have probabilities for ranges though so for example we can say the probability of x being between 3.13 and 3.15 is greater than zero we have a useful function called the cumulative distribution function or the cdf that helps us measure such probabilities we usually label this function as big f of x and so the definition of big f of x is it's the probability that the random variable big x is greater than negative infinity but less than little x note that the probability of big x being between negative infinity and positive infinity is 1 since x has to take on a value therefore the value of big f of positive infinity is equal to 1. now how about going back to our original problem if we want to calculate the probability that x is between 3.13 and 3.15 that would just be big f of 3.15 minus big f of 3.13 so now let's talk about the other useful function when we're talking about continuous variables this one's called the probability density function or the pdf we usually denote it by little f of x and it is defined as the derivative of big f of x with respect to x so it's like the slope of big f of x note that this function can be greater than 1 since it's not a probability it is a probability density f x little f of x does have to be greater than or equal to zero though so here's one example where little f of x can be bigger than one so let's say little f of x is uniform between zero and zero point one so that means if you try to sample from this random variable x you'll always get a value between 0 and 0.1 and the probability of any particular value is equal to all the others now i'm going to claim that little f of x has to equal 10 if x is between 0 and 0.1 and 0 otherwise now why is this because big f of x since little f of x is the derivative of big f of x big f of x is the integral of little f of x in the integral we can take the constant out and then calculate the integral from 0 to x now we know that from above big f of infinity has to equal one so the integral from minus infinity to infinity equals to one but since little f of x is zero after 0.1 we can just take the integral from 0 to 0.1 that gives us 0.1 c and if we solve for c c equals 10. therefore we've seen a scenario where little f of x can have a value greater than one because it's a probability density and not a probability value later on in this course we'll look at more complex continuous distributions hey guys and welcome back to this course on matlab and probability in this lecture we're going to talk about mean and variance so how do we measure the distribution of continuous variables we know that with discrete variables we can just check how often your random variable takes on a certain value and divided by the total number of values to give you an approximation or an estimation to that value's probability but we can't exactly do that with continuous variables we can't really count them put them in the buckets like discrete variables so we need to measure certain characteristics of the distributions that might tell us the shape of their distribution curve so two common measures that you probably have already heard of are the mean and the variance so first let's talk about the mean so the mean is like the average it means you add up all the different values and you divided by the total number of values and that kind of tells you what the middle value would be so what i want you to do is to load back up our random integer csv and so you know how to calculate the sum of all values and we want to divide this by the total number of values so the mean of r so let's just plot r again to remind ourselves what values it can take so minus five to five and so we get an average value of 0.35 so why is this value so small because we generated values from -5 to 5 with a uniform distribution we could say that distribution is balanced whether the value is less than zero or greater than zero so a lot of the numbers end up cancelling each other out when you sum them and therefore our mean value is about zero notice that we can never actually get the value 0.35 from our distribution right because our distribution gave us uniformly distributed numbers between minus five and five but only with discrete values so you could get negative five negative four negative three negative two negative one or one all the way up to five but you would never get the value 0.35 so it might seem odd that we also call the mean value the expected value even though we would never expect to get that actual value we only expect that to be the average value of the values that we do draw from this random distribution and of course there's an easier way to calculate the mean in matlab and it's just the function mean right so we get the same value now let's talk about the second measure of distribution variance so we already talked about the mean which is kind of like the middle value so we could say that it measures the centeredness of the distribution so where where the middle is where the center is variance does a different thing it measures the spread so so one thing tells us where the random variable goes and that's the mean and then the variance tells us how much it's spread out from that middle value so the definition of the variance is it's the expected value of the random variable minus its mean and then we square that whole thing and of course that doesn't help as much since it doesn't tell us how to calculate or estimate the variance but it's very similar to how we do the mean so let's set the mean of r to be mean r like that we can then calculate the variance of r by taking r minus the mean of r and so we want to do the dot product between these two which is effectively squaring all the individual values and then multiplying by the corresponding value like that so it's it's treating r as a vector and then finding the dot product with itself after subtracting the mean in other words it's like subtracting the mean and taking the squared distance or the squared length and of course we divide by the length of r so we get 10.6475 of course matlab has its own variance function so we didn't really need to do all this work and it's just var so we get a pretty similar value one thing that comes up in statistics which is a little bit outside of the scope of our discussion is that if we divide instead of by n we divide by n minus one that gives us and what we call an unbiased estimate of the variance and so this gives us the exact value of matlab's version of var hey guys and welcome back to this course on matlab and probability in this lecture we're going to talk more about the gaussian distribution so the first thing we'll ask is we've generated data so far from uniform distributions how can we generate data for random normal distributions so there's a function in matlab called random that will do this so if you type in random should give you different function signatures so let's say we want to generate a thousand random normal values okay and so this this can represent any any number of random random values so if you download data from kaggle or some other data set where you want to do machine learning or statistics on you want to know the distribution of your input in this case we want to know if distribution is normal or not so one thing we could do to check that is just to plot it plot the histogram right and so you can see that it looks relatively normal we can use more bins to get a finer granularity so we still see this same type of distribution even with 50 bins and of course if we increase n our curve looks closer and closer to the gaussian curve that we saw from the last lecture now of course in the real world your data is not going to look this nice and clean since your distribution may or may not be gaussian distributed the question now is how do you test if your data is gaussian distributed or not so we're going to talk a little bit about hypothesis testing but not too much since it's a pretty long and difficult subject so the basic idea with hypothesis testing is you have two different hypotheses so in this lecture we're not going to talk about the mechanics behind statistical testing we're going to kind of work backwards and jump right into statistical tests that you can use in matlab right away so the first is the shark baro test in matlab it's the function jb test so you can use it to return a hypothesis and a p-value the hypothesis will be whether or not your data is randomly distributed and it returns a p-value to tell you the strength of that hypothesis so let's try this on our data that we just generated all right so one thing that we need to talk about is how to interpret the return values of the jacques barra test so when we talk about hypothesis testing you'll see that there is a null hypothesis and an alternative hypothesis the jarg bearer test will return one if we reject the null hypothesis and the null hypothesis is that our data is normally distributed so if we pass in r we get the hypothesis is the null hypothesis and a p-value of 0.5 which means we do not reject the null hypothesis that our data is normally distributed there's another test called the kamalgrass myrnov test which essentially does the same thing so the null hypothesis again is that the data comes from a normal distribution and the result h will be 1 if you reject the null hypothesis so let's try that same thing with ks test right and so this also rejects or does not reject the null hypothesis that the data is normally distributed so now let's do something interesting let's generate some random data that we know is not normally distributed so we'll do this data from a uniform distribution so now i want to do jb test on the uniform data right so we get h equals 1 which means we do reject the null hypothesis and our p value is 1 1000th and usually a p-value is determined significant when it's less than five percent so this what this is basically saying is that our uniform generated data is not gaussian distributed which we know already is true we can do the same test with komogros mirnov and so this also rejects the null hypothesis with a much smaller p-value right and so this is how you can test if your data is normally distributed or not hey guys and welcome back to this course on matlab and probability in this lecture we're going to continue our talk about hypothesis testing of gaussian distributed data so to elaborate on how hypothesis tests work we're going to talk about the hypotheses a little bit more so there are two hypotheses when we're doing a statistical test there's a null hypothesis which we call h naught and it usually represents the control data or random data that results purely from chance the second hypothesis is the alternative hypothesis so this is the thing we're trying to prove generally for example if we're doing an experiment where we're testing a drug the drug actually working would be the alternative hypothesis and so the alternative hypothesis is the hypothesis that sample observations are influenced by some non-random cause so now suppose we have some random data so let's say r1 equals rand n say it as 100 points with a mean of zero and a variance of one now let's say we have a another data set which is maybe 20 points but this one's going to have a different mean so let's say it's mean is one and let's increase its variance a little bit okay so we have two distributions that are gaussian distributed the first one has zero mean and one variance the second one has a mean of one but a standard deviation of two so how do we compare these two distributions there's a test called the t-test or the two-sided t-test or the two-sample t-test which does what we want and it again returns a hypothesis and a p-value so we can try this t-test two two okay so we reject the null hypothesis in this case with a very very small p-value remember it only has to be less than five percent for us to reject the null hypothesis so let's try some things let's have less data points for r1 okay and let's do our t-test again so notice how we still reject the null hypothesis but our p-value has increased so it's less significant than before all right so now let's do the same thing for r2 let's say this now only has 10 points let's do the t-test again all right so this is still significant let's increase the variance all right so i had to increase the variance a lot to get an insignificant p-value so that's one thing about when you're comparing two gaussian distributions you can't really say one is bigger than the other if they're spread out a lot so let's put the variance back for r2 but let's say the mean is now less far away from our ones mean let's do our t-test again right so this also gives us an insignificant p-value so that's another fact about the t-test is you also can't tell if two distributions are different if they are very close together if they're very far apart so the mean is let's say we do the t-test again we now get a very small p-value [Music] so
Info
Channel: Scott D. Clary - Success Story Podcast
Views: 5,126
Rating: 4.9354839 out of 5
Keywords: matlab, mathworks, matlab tutorial, simulink, matlab tutorial for beginners, programming, matlab course, matlab simulink, matlab (programming language), tutorial, joseph delgadillo, complete matlab tutorial go from beginner to pro, free udemy course, math functions and symbolic data types, covid19, coronavirus, vacuna, vacunacovid, vacunas, noticias, peru, matlab basics, coding, learn matlab, how to learn matlab, engineering, matlab for beginners, complete matlab course, joseph, delgadillo
Id: uF7V7xPX7AM
Channel Id: undefined
Length: 265min 44sec (15944 seconds)
Published: Fri Feb 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.