The point of this post is to talk about doing mathematics in Matlab. This is an extension of our previous post on Kickstarting Matlab. You will learn how to use all mathematical operators and get to understand your Matlab second best friend (after the profiler) : the point or dot operator. We also briefly talk a little bit about Matlab history. My only hope is that after this, you will get the point.
Matlab is full-fledged programming language now. But back in the original days, it was mainly a gigantic calculator for crunching numbers. As each release passed, it was iteratively extended to included GUIs, Object-Oriented Programming, or more advanced functions. The list of all functions available in the first Matlab, as stated by one of its creator Cleve Moler, were the following :
ABS ANS ATAN BASE CHAR CHOL CHOP CLEA COND CONJ COS DET DIAG DIAR DISP EDIT EIG ELSE END EPS EXEC EXIT EXP EYE FILE FLOP FLPS FOR FUN HESS HILB IF IMAG INV KRON LINE LOAD LOG LONG LU MACR MAGI NORM ONES ORTH PINV PLOT POLY PRIN PROD QR RAND RANK RCON RAT REAL RETU RREF ROOT ROUN SAVE SCHU SHOR SEMI SIN SIZE SQRT STOP SUM SVD TRIL TRIU USER WHAT WHIL WHO WHY
My goal today is to help you used Matlab as it was originally intended to be used : to crunch numbers using mathematical operators.
We already saw that you can create an array in Matlab using the following commands :
>> X=[1 2 34];
>> X=[12 2 3;32 45 23];
It’s easy to do mathematics with these elements.
Y is obtained via a multiplication of all elements in X with the constant 2.
What happen when you multiply two matrices? Well, you have to remember your basic maths on that (wikipedia could help you out). The general rule is that both matrices need to have a dimension of the same length to be able to build this matrix product.
Which is why you will get the following results :
0.7264 1.6614 1.0149 1.2232 0.7816
0.5981 1.4775 0.7565 1.1739 0.8146
0.0887 0.2157 0.1147 0.1689 0.1155
Error using *
Inner matrix dimensions must agree.
As you can see, B*A works but not A*B : Matlab follows all these well-established mathematical principles.
Let’s suppose now that you don’t want this complicated matrix multiplication. You want EVERY elements in B to be multiplied by the same corresponding element in A. Now, for this to work, B and A MUST be the exact same size. Matlab provides a very very easy way to do that : the dot operator. It works like that :
The ‘.’ modifies the operation done by the * into an element-wise multiplication : each element in A is multiplied with the corresponding element in B.
In my day to day coding, I actually uses ‘.*’ much more often than ‘*’. This is a very handy operator to vectorize your code.
I am sure you will love to know that this ‘.’ trick also work with ‘/’ :
Now that we are talking about division, check this out :
0 3.5191 -4.2211
0 0.6222 0.4930
0 -2.0752 4.0228
As you can tell, B/A also work but is very different to B./A. It is basically trying to solve the equation x*A = B. Similarly B\A is trying to solve A*x= B. These are very very different operations. These are NOT element-wise operators.
Before I leave you to revise your matrix algebra, I need to say a few words about the power operator ‘^’. Similarly, to do an element-wise operation, you need to modify it with the ‘.’. Indeed :
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
Y^2 does not work because in effect, it is trying to do Y*Y and the size of Y does not allow this matrix multiplication.
Be very careful with that, it can be misleading :
A^2 is the same size as A.^2 but is quite different. Indeed A^2=A*A is very different to A.^2=A.*A. You can crash a space probe on Mars with such a programming mistake with no error message. All for the cost of one single tiny point.