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.