## Learn Matlab Episode #3: Basic Arithmetic

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.

## Learn Matlab Episode #2: Basic Syntax

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.

## Learn Matlab Episode #1: Installation & Resources

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

## 3 FREE Udemy Courses for Black Friday!

3 FREE technical courses for Black Friday! Coupons expire 11/25/16.

If you want to get started as an ethical hacker and build a keylogger using C++, you will LOVE this course!

Build a Keylogger for Ethical Hacking!

Learn to program using C++ with this FREE Udemy course!

Get Started Coding with C++!

Learn to program web application using Angular 2!

Get started using Angular 2!

## Python 3 Final Project

Enroll in The Complete Python 3 Course: Beginner to Advanced!

## Modules Explained in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

## Loops Explained in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

## If Else Statements in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Alright guys, now we’re going to be learning about some conditional statements in Python. So we’re going to be learning about the if-else statement, and this basically provides a way to evaluate if something is true or false, or if something is something, or it’s not something and can do something different based on which condition is true. So let’s go ahead and let’s set “check = False” and what we’re going to do with that is we’re gonna say “if check == false:” “print(“It is false”)” and I’m going to save this and this is the minimal you need for an if statement. We’re going to go ahead and run this and it’s going to say it is false because it is false. Now if we were to change this to true, and run the same code, it’s not going to do anything, and that’s because we haven’t provided and else. So if it’s equal to false it will do this otherwise it’s just going to do nothing. So let’s go ahead and drop back four spaces to where the if statement began, I guess, and that type “else:” and we’re going to “print(“It is actually equal to True”)” So let’s go ahead and let’s run the script and you’ll see that now where this is not the case, check is not equal to false, so then it’s going to do this instead. Now, what we’re going to do is this is good if you want two conditions, but what if you want more the two conditions? Between the if and else you can actually use something called “elif:” and here we actually need to supply a condition to the elif. So think of it like another level to the if statement, and think of the else as a catch-all like if none of the above are true than just do this. So if check equals false it’s gonna do that. “elif_check == “Hamburger”:” we are going to “print (“Yummm, hamburgers”)” and let’s throw another “elif_check == “Yo”:” and say “print(“Hello”)” So let’s go ahead and save this, we’re going to run it again, and as you can see check is equal to true. So it says is this true? Nope, move on. Is this true? Nope, move on. Is this true? No, move on. Okay, well the else is going to say well if none of the above is true them we’re just going to do this. So what we’re going to do right now is we’re going to set this to “check = Hamburger” and we’re gonna run the script, and you’ll see what’s happening is it’s checking this one first, that’s not true, so it moves on its like okay this one’s true so let’s just do this and then let’s get out of the if statement, so it doesn’t check any further. So that’s what an if statement is in Python and that’s necessary for what we are about to do.

## Return Values in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!