I still remember the wonderful feeling when I joined the aerospace industry and started to code in Matlab. It felt so easy to write in a few seconds a function that would have taken me at least twice the effort in FORTRAN 77. Yes, you are reading right, that old-school programming language. Believe it or not, FORTRAN is still nowadays the backbone of many legacy tools of aerospace programs of the ’90s.
Over time, these legacy tools are being abandoned in favor of a new toolset coded in modern programming languages, not because these legacy tools are useless, but because they seem like rigid black-boxes for the new generation of engineers.
Is in these cases when Matlab can be a good choice, with a simple syntaxis, a fast learning curve, a hundred toolboxes, incredible documentation, and a very active community of programmers. So, it seems like a very good candidate, right? Well, as always, it depends on the specific requirements for these tools, and sometimes speed is a must.
Although Matlab is great for prototyping and interactive analysis, in some applications it might seem too slow.
So in case you "feel the need for speed", here are five suggestions to improve the performance of your MATLAB code and make you feel like a "Jedi-programmer".
1. System Calls with jsystem
In case your Matlab code makes a lot of calls to system functions, the performance drop might be noticeable because of the large overhead that comes with MATLAB’s system function. This overhead can be bypassed using the much faster jsystem function created by avivrosenberg based on Java.
This is an easy drop-in for the system function as it has the same syntaxis and also gives you more flexibility to select a specific shell to run the commands.
2. Matrix For Loops
Computer memory stores data using sets of single-dimension arrays. By default, Matlab stores these elements with a column-major array layout. And what does it mean? In a nutshell, in a column-major order scheme elements of each column are contiguous in memory. Conversely, in a row-major array layout (C, C++, or Python), contiguous elements in memory correspond to elements of each row.
In order to speed up the code, it is important to access an array element in the proper order to minimize large leaps between non-contiguous elements.
The golden rule for MATLAB is to always assign the index of the faster loop to the most left part of the matrix dimensions and the slowest to the most right part. This rule also applies to tensors.
For example, in the following matrix elements assignment, we obtain a speedup of nearly x6 when using the column-major order. A nice and simple trick to add to your day-by-day programming rules.
3. 1D Linear Interpolation
No matter if you are writing a script for making scientific computations, stock market analysis, or statistics, at some point you will need to make use of interpolation functions to obtain information about intermediate points not covered by your data.

For the specific case of single dimension linear interpolation, Matlab has the interp1 function. But once again, interp1 has been demonstrated to be slow in many applications of linear interpolation.
Performances of interp1 can be boosted using histc function _ to locate the indexes of the closest individuals in the x breakpoints vector. This smart trick has been applied in the core of the interp1qr_ function created by Jose M. Mier achieving an incredible x11 boost in performance as shown in the following script.
4. Do not Use Logical Temporary Arrays
Many times when working with large sparse arrays I have a need to check for any non-zeros elements, like in this example:
It might not seem a lot of time for Matlab to compute this operation, but if you have large arrays, and this zero-test is repeated many times in your code, computation time might start to build up.
When looking for any non-zero element in an array, you might expect Matlab to stop the search almost immediately once the first non-zero occurrence is found, right? But this is not "immediately" when you use logical temporary arrays like a(:)~=0.
When a logical temporary array is used in this expression, Matlab incurs a large overhead to first convert numerical values to a logical array.
To overcome this overhead, we can take advantage of the fact that numerical values can be used as logical. So the tip is to do not use temporary logical arrays when comparing to zero.
In the next example, you can see how the previous simple code can be optimized to obtain an incredible x337 speed up using this tip!
5. Using Mathematical Theory to Speed Up Convolutions
Matlab’s implementation of convolution in functions like conv, conv2, and convn relies on the moving window approach, using automatic multi-threading to improve performances (only when Matlab has not been initialized with the flag "-singleCompThread", see more information on how to use this flag here).
Nevertheless, we can speed up the computation of convolutions if we apply the original Convolution Theorem, which states that conv(a,b) = ifft(fft(a,N) .* fft(b,N)), an idea originally proposed by Bruno Luong.
In the following example we need to remember to zero-pad the data to get comparable results:
In this example, we have obtained an incredible x1100 speed up with respect to the conv function. That’s really a big improvement!
Additional References to Speed Up Your Code
Rodney Rodríguez Robles is an aerospace engineer, cyclist, blogger, and cutting edge technology advocate, living a dream in the aerospace industry he only dreamed of as a kid. He talks about Coding, the history of aeronautics, rocket science, and all the technology that is making your day by day easier.
_Please check me out on the following social networks as well, I would love to hear from you! – LinkedIn, Twitter._