MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

11 Improving Performance and Memory Usage Techniques for Improving Performance This section covers the following suggestions on how you can improve the performance of your MATLAB programs: • “Multithreaded Computation in MATLAB” on page 11-4 • “Vectorizing Loops” on page 11-4 • “Preallocating Arrays” on page 11-7 • “Coding Loops in a MEX-File” on page 11-9 • “Assigning to Variables” on page 11-9 • “Operating on Real Data” on page 11-10 • “Using Appropriate Logical Operators” on page 11-10 • “Overloading Built-In Functions” on page 11-11 • “Functions Are Generally Faster Than Scripts” on page 11-11 • “Load and Save Are Faster Than File I/O Functions” on page 11-11 • “Avoid Large Background Processes” on page 11-11 Multithreaded Computation in MATLAB One way of achieving improved performance when running MATLAB on multicoreormultiprocessorsystemsisto take advantage of its multithreaded computational capabilities. To enable multithreaded computation in MATLAB, see “Multithreading Preferences” in the “Desktop Tools and Development Environment” documentation. Vectorizing Loops MATLAB is a matrix language, which means it is designed for vector and matrix operations. You can often speed up your M-file code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations. Simple Example of Vectorizing Here is one way to compute the sine of 1001 values ranging from 0 to 10: 11-4

Techniques for Improving Performance i = 0; for t = 0:.01:10 i = i + 1; y(i) = sin(t); end A vectorized version of the same code is t = 0:.01:10; y = sin(t); The second example executes much faster than the first and is the way MATLAB is meant to be used. Test this on your system by creating M-file scripts that contain the code shown, and then using the tic and toc functions to time the M-files. Advanced Example of Vectorizing repmat is an example of a function that takes advantage of vectorization. It accepts three input arguments: an array A, a row dimension M, andacolumn dimension N. repmat creates an output array that contains the elements of array A, replicated and “tiled” in an M-by-N arrangement: A = [1 2 3; 4 5 6]; B = repmat(A,2,3); B = 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6 4 5 6 repmat uses vectorization to create the indices that place elements in the output array: function B = repmat(A, M, N) % Step 1 Get row and column sizes [m,n] = size(A); 11-5

Techniques for Improving Performance<br />

i = 0;<br />

for t = 0:.01:10<br />

i = i + 1;<br />

y(i) = sin(t);<br />

end<br />

A vectorized version of the same code is<br />

t = 0:.01:10;<br />

y = sin(t);<br />

The second example executes much faster than the first and is the way<br />

<strong>MATLAB</strong> is meant to be used. Test this on your system by creating M-file<br />

scripts that contain the code shown, and then using the tic and toc functions<br />

to time the M-files.<br />

Advanced Example of Vectorizing<br />

repmat is an example of a function that takes advantage of vectorization. It<br />

accepts three input arguments: an array A, a row dimension M, andacolumn<br />

dimension N.<br />

repmat creates an output array that contains the elements of array A,<br />

replicated and “tiled” in an M-by-N arrangement:<br />

A = [1 2 3; 4 5 6];<br />

B = repmat(A,2,3);<br />

B =<br />

1 2 3 1 2 3 1 2 3<br />

4 5 6 4 5 6 4 5 6<br />

1 2 3 1 2 3 1 2 3<br />

4 5 6 4 5 6 4 5 6<br />

repmat uses vectorization to create the indices that place elements in the<br />

output array:<br />

function B = repmat(A, M, N)<br />

% Step 1 Get row and column sizes<br />

[m,n] = size(A);<br />

11-5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!