Posted on

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

Click here to subscribe for more videos like this!

Loops in MATLAB

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

Posted on 1 Comment

Learn Matlab Episode #5: Linear Algebra

Click here to subscribe for more videos like this!

Linear Algebra in MATLAB

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 A(X) + 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 = [3 2 -1; 2 -2 4; -1 0.5 -1]; OK, so that’s A, b = [1 -2 -2]; ok. So, when you’re working with equations like this you might be tempted to do just simple algebra, so you could say x = inv(a)*b because mathematically that works. So, I can do that oh and right I have to make b a column matrix, so a*b. Sorry, I used the wrong b so inv(A)*b so I get 1 -2 -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. It’s 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’s a forward slash division and a backslash division. So, this is the use case where you 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/a, but since they’re matrices we write the answer like this so A\b. So, 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 inv(a)*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 det, ok. So remember A = [3 2 -1; 2 -2 4; -1 0.5 -1]; from the last example we can also compute the eigenvalues of a matrix. So, iaeig and now you know when we compute the 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, its dynamic. So if we look at the documentation for the eig 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 so you can see these different function signatures. So if I assign the returned values of eig 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 a columns of the matrix V. So let’s try that. [V,D] = eig(A) so we get a diagonal matrix D which has the same eigenvalues as before, and then the eigenvectors are all in V. So let’s just confirm that the definition of eigenvalues and eigenvectors holds, so A*V should equal V*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 == as we do in regular programming. So 1 == 1 is 1 which means true, 1 == 0 is false. So if we do A*V == B*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 is always in programming some round off error. So if we subtract A*V – V*D we should get back a very small number, alright. So, we do that we see 1×10^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 D 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 with matrices we call that the norm, and so typically all the elements so 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 numbers, so 5×10^-15 which we again can use to confirm that it’s very close to zero, so A*V is very close to equal to V*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 2, 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, 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 =3 you can only use P = 1 to infinity or fro, and so one would be the Manhattan distance.

Posted on

Learn Matlab Episode #4: Functions & Constants

Click here to subscribe for more videos like this!

QUESTION: What are the popular builtin functions/constants in MATLAB? How do you define your own functions?

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 0 is going to be 0, I can do cosine of 0 gives me 1, tangent of 0 of course would be also 0. 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 grid before 1,2,3,4, if I pass that to sine what that does is it calculates the sine of every element of A and returns a matrix the same size as A. So I can do all 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. If I do log of exp of one I should get back one. I can do square root is sqrt ok, 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 alright, and so now if I type pi again it’s now back to original pi. Generally, I would avoid overriding 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 one which is e to the power of 1 which is e. Another important constant is i. So if I do the square root of minus one 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*i which should give you -1, or you can do i^2. So you do exponents using the hat symbol so that also gives me -1. 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*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 3 by 3 identity matrix. I could do something like this which would be a little bit inefficient right, or I could do eye(3) 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, what I can do is 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 can 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 wanna 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 will 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 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 of the Y. So matlab has some useful syntax highlighting it will 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 command s or go to file save. So we’ll save it. So one thing to notice you have to save the function name has to be the same as the filename, and so like I mentioned before matlab already has its own sine H so we don’t want to overwrite matlab sine H. So we’re going to call our sine H something else, I’m going to call it my sine H. I’m going to rename this file to my sine H. Alright, so now I’m not getting that warning anymore. Ok, so we’re going to define our own hyperbolic sine 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. 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 is 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 % sign to comment code. So I’m going to save that, I’m going to use this function so my sine H let’s say one, zero so we can see from this image that hyperbolic sine crosses zero and then it increases as x increases, so that’s what you should be seeing. Remember that matlab works with matrices though so if I wanted to I can say X goes from 0 to 10 and I want to calculate the hyperbolic sine for all of those values I can just pass in X and I’ll get back hyperbolic sine for all of those values. 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 and so you can see matlab has already 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. Ok, 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, and I’m going to add a comment returns 1 if X is even. Ok, 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 % sign is used for comments we do not use the % for modulo, if you’ve done that in c or java before. So instead we use modulo by calling a function called mod. So, basically it returns us the remainder. Ok, so this should be very simple. We want to return 1 if X is even so we know we want to mod 2, so y = mod(X, 2). Ok, but we have a problem here because when you take the modulo of an even number with two you will get 0, but if you take an odd number modulo 2 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, until the 0 gives us one, until the one gives us 0, so what we want is to put the tilde in front of the mod, ok. And so now if I call is_even(1,2,3,4,5) we should get ones in the spots where the input is even which we do. So for 2 and 4 we get 1, for 1, 3 and 5 we get 0.

Posted on

Learn Matlab Episode #3: Basic Arithmetic

Click here to subscribe for more videos like this!

QUESTION: How do you perform basic arithmetic in MATLAB?

So in this matlab tutorial we’re going to go through some basic arithmetic in matlab. First I’m 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, OK. So suppose I want to add two matrices, that’s very simple A + B. I want to subtract two matrices A – B. I want to multiply two matrices that’s A * B. So one thing to note about multiplication is this is pure 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 it has columns it’s not going to work. So, the size(B2) is 3 by 2 and the size(A) is two by two, so if I try to multiply A * B2 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, I transpose B2 it’s 2 by 3 and so a two by two matrix times a 2 by 3 matrix will give me a 2 by 3 matrix. So you can also divide matrices with the forward slash, 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 which we are going to cover in a later tutorial. By the way, if you want to divide two numbers that’s very simple we use the / 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 + B * 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 lets just look at what we have for A & B again. So that’s pretty simple in matlab you just put a .* and so it multiplies each element by each corresponding element in the other matrix. This also works for division so a ./ So if you remember from your linear algebra classes there some different types of multiplication that you can have when it comes to matrices and vectors. First one were going to talk about is the inner product. I have x = (1, 2, 3) y = (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 will cover functions more in a later lecture but I will 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 1 by 3 vector, and so the result would be a three-by-three vector. 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 see x times y transpose. So, just the opposite since I’ve been using row vectors. Lastly, there is the cross product and so 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. Alright, 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 and so that’s pretty simple to do in matlab, also. It’s just a function called cross.

Posted on

Learn Matlab Episode #2: Basic Syntax

Click here to subscribe for more videos like this!

MATLAB Basic Syntax Tutorial

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’s 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 filename 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 the path for your particular project. So in matlab there are three basic types of numbers that you’ll be working with, or variables, that are all actually matrices. So you have 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. So as I’ve mentioned before in matlab all these variables are represented as matrices. So if I do something like x = 5 you see that in a regular programming language you might just think of this as x = 5, but I can do something like check the size(x) and I see that it’s actually a 1 by 1 matrix. I can create a vector, 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 v = [1,2,3]; So if I do size(v) 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 one by three matrix with the elements 123. 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 two by two matrix now with the elements 1234 in the variable A, and if I do size(A) it’s two by two. So those are the two things we’ve learned about initializing vectors and matrices 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 semi-colons. Another thing is that when you’re working with matrices and vectors
and you’re multiplying them and stuff like that, the dimensions of the vector are very important. So, typically you know you 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. And 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. Ok, 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 i row in the j column it would be a(i,j) So if I want a(1,1) for example I would do that. If I want a(1,2) and so on a(2,1), 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 where 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 world of A. Similarly 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 will just return me the entire A. So let’s create a bigger matrix now so I can better illustrate my next point. Alright, so I just created a four by four matrix. So the colon syntax can also be used to access ranges. So if I want I look at A it’s 1-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 in the second and third column. If you’ve programmed before usually when you’re working with vectors, matrices, and arrays they’re zero indexed, in MATLAB it’s one indexed. So, a 1-1 returns me the first element, it’s not a 0-0. Another 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:10 gives me back all the numbers from one to ten, 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 simpler to do than typing one through ten 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 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.

Posted on

Learn Matlab Episode #1: Installation & Resources

Click here to subscribe for more videos like this!

QUESTION: Where can you get MATLAB, add-ons, help with questions, etc?

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 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 will 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 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. Ok, so let’s talk about getting matlab. So I’ve already opened the website, if you scroll down to the bottom, or near the bottom, you can see a section called try or buy. So you can try matlab for free probably a trial that lasts 30 days…let’s check. It doesn’t say you have to enter your email but 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 its over $2,000, and if you’re part of an educational institution you can use the education tab and get matlab for $500, the home version is just for personal use that’s a $149, and the student version is $49 so you probably have to go through some process to prove that you’re 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 201 a version of matlab. So one thing you’ll notice is for 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 bio informatics you may want to purchase the bio informatics toolbox. So you can click on the links and get an idea of what’s included in the packages, maybe you do bio informatics and you don’t really even need the functions that the tool box 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 they’re 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 is 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 stackoverflow results so it wouldn’t only be matlab central which might be helpful.

Posted on 4 Comments

The Complete MATLAB Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

I hope you enjoy this FREE complete Matlab course! In this tutorial we will over the following topics:

  • What is Matlab, how to download Matlab, and where to find help
  • Introduction to the Matlab basic syntax, command window, and working directory
  • Basic matrix arithmetic in Matlab including an overview of different operators
  • Learn the built in functions and constants and how to write your own functions
  • Solving linear equations using Matlab
  • For loops, while loops, and if statements
  • Exploring different types of data
  • Plotting data using the Fibonacci Sequence
  • Plots useful for data analysis
  • How to load and save data
  • Subplots, 3D plots, and labeling plots
  • Sound is a wave of air particles
  • Reversing a signal
  • The Fourier transform lets you view the frequency components of a signal
  • Fourier transform of a sine wave
  • Applying a low-pass filter to an audio stream
  • To store images in a computer you must sample the resolution
  • Basic image manipulation including how to flip images
  • Convolution allows you to blur an image
  • A Gaussian filter allows you reduce image noise and detail
  • Blur and edge detection using the Gaussian filter
  • Introduction to Matlab & probability
  • Measuring probability
  • Generating random values
  • Birthday paradox
  • Continuous variables
  • Mean and variance
  • Gaussian (normal) distribution
  • Test for normality
  • 2 sample tests
  • Multivariate Gaussian