vectorization_tricks - CUED: Matlab vectorisation tricks...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CUED: Matlab vectorisation tricks 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 the comp.soft-sys.matlab newsgroup 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 [email protected] . 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))]. If X and V are the same size and V only consists of 1s and 0s then MATLAB interprets V as a mask, and returns only the elements of X whose position corresponds to the location of a 1 in X. For example, if X is an array, then X>6 is an array the same size as X with 1s where the corresponding element in X is >6 and 0s elsewhere. This array can be used as a "mask" to select only the elements in X which are >6. Try X=1:10 V=X>6 X(V) or, more succinctly, X=1:10 X(X>6) There can be a problem if MATLAB decides to use the masking scheme when you want index addressing but such situations are rare. Creating and manipulating Matrixes To use the indexing ideas effectively you need to be able to create 'mask' matrices efficiently, and manipulate arrays. This requires the use of functions that you may not have used before. Some are listed here. Array Manipulation - look at flipud , fliplr , rot90 , triu (extracts upper triangle), tril (extracts lower triangle), filter . Array Creation - look at hankel (try hankel(1:5) , for instance), kron toeplitz , diag , sparse , repmat (replicates and tiles matrices) and find . There's a trick for duplicating a vector of size M by 1, n times, to create a matrix of size M by N . In this method, known as Tony's Trick, the first column of the vector is indexed (or referenced, or selected) n times. v = (1:5)'; X = v(:,ones(3,1)) (1 of 7) [4/25/2002 8:51:52 PM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CUED: Matlab vectorisation tricks Examples These examples are short, so by reading about the functions used and testing with small matrices, you should be able to discover why they work. More than one way is shown to solve some of these questions so that you can compare methods. When big matrices are used, you might find that some methods are hundreds of times faster than others. Remember that the most elegant- looking way may not be the fastest. Use
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2011 for the course MATH 571 taught by Professor Staff during the Winter '08 term at University of Michigan.

Page1 / 7

vectorization_tricks - CUED: Matlab vectorisation tricks...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online