GPUmat: GPU toolbox for MATLAB. Cambridge University Engineering Department - Matlab vectorisation tricks. Some basic tips on speeding up matlab code and on exploiting vectorisation are mentioned in the Optimisation section of our matlab page. Some of the tricks below come from newsreader.mathworks.com and the Mathworks site. They are low-level and capable of delivering order-of-magnitude improvements.
I've added the author names (where known). If you have contributions, mail them to tpl@eng.cam.ac.uk. Indexing using vectors Many of these tricks use the fact that there are two ways of accessing matrix elements using a vector as an 'index'. If X and V are vectors, then X(V) is [X(V(1)), X(V(2)), ..., X(V(n))]. There can be a problem if MATLAB decides to use the masking scheme when you want index addressing but such situations are rare.
You can combine what you already know to solve some problems. M=magic(5) creates a 2D matrix that you can practice with. M>8 is part of the way towards an answer. Sum(m>8) That's closer - sum has summed the columns. Sum(sum(m>8)) you'll get the answer. Sum(m(:)>8) Now. Programming Patterns: Maximizing Code Performance by Optimizing Memory Access - MathWorks News & Notes - June 2007. Most MATLAB users want their code to be fast, especially when it is processing very large data sets. Because memory performance has not increased at the same rate as CPU performance, code today is often “memory-bound,” its overall performance limited by the time it takes to access memory. Fortunately, with a little knowledge of how MATLAB stores and accesses data, you can avoid inefficient memory usage and improve the speed of your code. This article describes three ways to improve the performance of memory-bound code: Preallocate arrays before accessing them within loopsStore and access data in columnsAvoid creating unnecessary variables In each case we will compare the execution speed of a code segment before and after applying the technique.
To ensure the best performance, the code segments are all timed in function M-files, not script M-files. Preallocate Arrays Before Accessing them Within Loops Figure 1. Why Code Segment 2 is Faster Store and Access Data in Columns Figure 2. Figure 3. Support - Code Vectorization Guide. Techniques for Improving Performance Preallocating Arrays for and while loops that incrementally increase the size of a data structure each time through the loop can adversely affect performance and memory use. Repeatedly resizing arrays often requires MATLAB® to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks. Often, you can improve code execution time by preallocating the maximum amount of space required for the array.
The following code displays the amount of time needed to create a scalar variable, x, and then to gradually increase the size of x in a for loop. tic x = 0; for k = 2:1000000 x(k) = x(k-1) + 5; end toc Elapsed time is 0.301528 seconds. If you preallocate a 1-by-1,000,000 block of memory for x and initialize it to zero, then the code runs much faster because there is no need to repeatedly reallocate memory for the growing data structure. tic x = zeros(1, 1000000); for k = 2:1000000 x(k) = x(k-1) + 5; end toc. Advanced Matlab. MATLAB Central - Home.