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

As you can see, this was a tool to do mathematics. Already within the first Matlab you could do PCA using the EIG function. The Matlab logo itself is an eigenfunction of wave propagation equation.

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];

or

>> X=[12 2 3;32 45 23];

It’s easy to do mathematics with these elements.

>> Y=2*X;

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 :

>> B=rand(3,2);

>> A=rand(2,5);

>> B*A

ans =

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

>> A*B

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 :

>> B=rand(3,2);

>> A=rand(3,2);

>> B.*A

ans =

0.3581 0.7270

0.8553 0.4873

0.5377 0.0140

The ‘.’ **modifies** the operation done by the * into an element-wise multiplication : each element in A is multiplied with the corresponding element in B.

For the record, ‘*’ is the exact equivalent of the function ‘mtimes‘ in Matlab, while ‘.*’ is the equivalent of ‘times‘. I invite you to check the corresponding documentation.

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 ‘/’ :

>> B./A

ans =

0.4967 1.2663

0.9805 0.8824

1.1672 0.0910

Now that we are talking about division, check this out :

>> B/A

ans =

0 3.5191 -4.2211

0 0.6222 0.4930

0 -2.0752 4.0228

>> B\A

ans =

0.7512 0.4354

0.4993 0.5758

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.

The corresponding function are rdivide or ‘./’ , mrdivide or ‘/’ and mldivide or ‘\’.

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 :

>> Y^2

Error using ^

Inputs must be a scalar and a square matrix.

To compute elementwise POWER, use POWER (.^) instead.

>> Y.^2

ans =

0.7210 0.5742

0.8723 0.5522

0.4607 0.1538

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=rand(2,2);

>> A^2

ans =

0.0812 0.1069

0.0508 0.6826

>> A.^2

ans =

0.0767 0.0094

0.0021 0.6781

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.