Find us on GitHub

Teaching basic lab skills
for research computing

MATLAB: Linear Algebra

MATLAB/Linear Algebra at YouTube

Hello, and welcome to another episode of the Software Carpentry lecture on MATLAB programming. In this episode, we'll have a look at how to do linear algebra.

The basic data type in MATLAB is the multi-dimensional array of floating point numbers, and these arrays should be treated like matrices from linear algebra. In higher dimensions, we can think of them as tensors.

For instance, if we multiply the arrays a and b, the product is found by the rules of matrix multiplication.

Aside from traditional operators, there are also a lot of functions that operate directly on matrices. For instance, sum can be used to add up all of the entries along one dimension of a matrix. In the first example, we are summing over each column of the matrix, which happens to be the default. In the second case, we sum over each row. Dimensions in a matrix are numbered 1 through n, and they are in the same order as the indices you use to access a single element.

Here's a long example of what we can do with array operations. Suppose we have been observing the progress of a disease in some test subjects. Each row of our array corresponds to one patient. And each column is an hourly count of responsive T cells. This means that the first column of our data is the initial T cell count for all patients, while the first row is all hourly samples for patient 0.

The mean of the data along axis 1, gives us the average across all patients for each hour. This gives us the "normal" progress of the disease. And the mean along axis 2 gives us the average T cell count per patient across all times, which could be useful if we need to normalize the data.

It might be even more interesting to look at what happened to people who started with no responsive T cell count at all. The first step is to select the first column of data, i.e., the initial T cell counts for each patient. If we compare these to zero, we get a Boolean array with True for each row of the array that meets our criteria. If we use this to index the original array, we get the two rows for which the count at time zero is zero.

Now let's find the mean T cell count over time for those people. Once again, we start by selecting column 1…

…and testing it to create a Boolean mask.

Using that mask as a subscript gives us the rows that have zero in the first place.

We can now use the 'mean' function along axis one—i.e., across patients.

Which gives us the average behaviour of patients who started with no responsive T cells at all.

This example highlights the key to good matrix programming: write high-level statements without loops, and let the computer worry about how to do the operations element by element. This is just as true for MATLAB or R as it is for Python:

Finally, always remember that you're not the first person to program with matrices. Always take a look at the online documentation for MATLAB before writing any functions of your own. The library includes routines to conjugate, convolve, and correlate matrices, to extract diagonals, calculate FFTs, gradients, histograms, and least squares. It can find roots, solve sets of linear equations, and do singular value decomposition. These functions are all faster than anything you could easily write, and what's more, someone else has tested and debugged them.

All of the functions in MATLAB have detailed help information accessible by typing help then the function name. You can also open the graphical help window from the menu on the graphical interface. In many cases, the documentation includes a brief introduction to the mathematical theory represented by a function as well as a description of various subalgorithms that might be used to handle special cases.

There are other resources to help you find MATLAB functions. One of the helpful features in MATLAB is the MATLAB Central file exchange, which includes thousands of functions for almost any type of calculation you could think of. Many of the functions in MATLAB Central are based on published research. Of course, like any software you use, you should be sure to investigate whether the functions have been tested. If you can't find a function to perform the calculation you need in MATLAB or on File Exchange, the next lecture will teach you how to write your own.

Thanks for listening!