TechNote 1109: How Do I Vectorize My Code?
1109
How Do I Vectorize My Code?
Revison: 1.3
Last Date Modified: 31August2000
This technical note provides an introduction to vectorization techniques. In
order to understand some of the tricks available, an introduction to MATLAB
indexing is provided. Then several vectorization techniques are discussed, in
order of simplest to most complicated.
There are two aspects to this presentation. Knowledge of all the techniques
available is only half of the battle. The other half is knowing when to use them 
recognizing situations where this approach or that one is likely to yield a better
(quicker, cleaner) algorithm. Each section provides an example, which
proceeds from a description of the problem to a final solution. We hope that
illustrating the process by example achieves both of these goals.
1. MATLAB Subscripting
A powerful feature of MATLAB is the ability to select subsets of an array or
matrix. There are two types of subscripting available in MATLAB.
In
indexed subscripting
, the values of the subscript are the indices of the matrix
where the matrix's values are desired. Thus, if A = 1:5, then A([3,5]) denotes
the third and fifth elements of matrix A:
» A = 1:5;
» A([3,5])
ans =
3 5
The second type of subscripting is
logical subscripting
. With this type of
subscripting, the subscript is a matrix the same size as A containing only 0's
http://www.mathworks.com/support/technotes/1100/1109.shtml (1 of 13) [4/25/2002 2:47:46 PM]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentTechNote 1109: How Do I Vectorize My Code?
and 1's.
The elements of A that are selected have a '1' in the corresponding position of
the subscripting matrix. For example, if A = 1:5, then A(logical([0 0 1 0 1]))
denotes the third and fifth elements of A:
» A = 1:5;
» A(logical([0 0 1 0 1]))
ans =
3 5
This second type of subscripting is very powerful, and we use it frequently in
the following sections. For more information on subscripting, see the
Using
MATLAB
manual.
2. Basic Array Operations: y(i) = fcn(x1(i), x2(i), .
..)
The simplest type of vector operations in MATLAB can be thought of as
bulk
processing
. In this approach, the same operation is performed for each
corresponding element in a data set (which may include more than one
matrix).
Suppose you have some data from an experiment. The data measurements
are the length L, width W, height H, and mass M of an object, and you want to
find the density D of the object. If you had run the experiment once, you would
just have one value for each of the four observables (i.e., L, W, H, and M are
scalars). Here is the calculation you want to run:
D = M/(L*W*H)
Now, suppose that you actually run the experiment 20 times. Now L, W, H, and
M are vectors of length 20, and you want to calculate the corresponding vector
D, which represents the density for each run.
In most programming languages, you would set up a loop, the equivalent of the
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '08
 STAFF
 Matrices, Tony

Click to edit the document details