Matlab Untuk Statistika
Matlab Untuk Statistika
Contents
Page
1 Introduction
2 The Proler
3 Array Preallocation
4 JIT Acceleration
5 Vectorization
14
7 Referencing Operations
16
8 Solving Ax = b
19
9 Numerical Integration
22
10 Signal Processing
26
11 Miscellaneous
29
12 Further Reading
31
Introduction
Matlab is a prototyping environment, meaning it focuses on the ease of development with language
exibility, interactive debugging, and other conveniences lacking in performance-oriented languages like
C and Fortran. While Matlab may not be as fast as C, there are ways to bring it closer.
Before beginning our main topic, lets step back for a moment and ask ourselves what we really want. Do
we want the fastest possible code?if so, we should switch to a performance language like C. Probably
more accurately, we want to spend less time total from developing, debugging, running, and until nally
obtaining results. This section gives some tips on working smart, not hard.
M-Lint Messages
In more recent versions of Matlab, the integrated editor automatically gives feedback on possible code
errors and suggested optimizations. These messages are helpful in improving a program and in learning
more about Matlab.
for k = 1:NumTrials
r = rand;
x(k) = runsim(r);
x might be growing inside a loop. Consider preallocating for speed.
end
hist(x);
Hold the mouse cursor over underlined code to read a message. Or, see all M-Lint messages at once
:::::::::::::::
with Tools M-Lint Show M-Lint Report.
Organization
Use a separate folder for each project.
A separate folder for each project keeps related things together and simplies making copies and
backups of project les.
Write header comments, especially H1.
The rst line of the header comment is called the H1 comment. Type help(cd) to get a listing
of all project les and their H1 comments.
If you nd yourself repeating certain commands on the console, save them as a script. Less typing
means fewer opportunities for typos.
This is an unfortunate common practiceany important variables in the base workspace will be
irretrievably lost.
Beware of clobber.
File clobber refers to the kind of data loss when a le is accidentally overwritten with another
one having the same lename. This phenomenon can occur with variables as well:
>> result = big operation(input1);
...
>> result = big operation(input2);
Variable result was clobbered and the rst output was lost.
Beware of what can crash MATLAB.
While Matlab is generally reliable, crashes are possible when using third-party MEX functions
or extremely memory-intensive operations, for example, with video and very large arrays.
Now with good working habits covered, we begin our discussion of writing fast Matlab code. The rest
of this article is organized by topic, rst on techniques that are useful in general application, and next
on specic computational topics (table of contents is on the rst page).
The Proler
Matlab 5.0 (R10) and newer versions include a tool called the proler that helps identify bottlenecks
in a program. Use it with the profile command:
profile
profile
profile
profile
on
off
clear
report
To analyze the eciency this function, rst enable and clear the proler, run the function, and then
view the prole report:
>> profile on, profile clear
>> example1(5000);
>> profile report
There is a slight parsing overhead when running code for the rst time; run the test code twice and time
the second run. The profiler report command shows a report. Depending on the system, proler
results may dier from this example.
3.09 s
4
0.016 s
Function List
Name
Time
Time
example1
3.09
100.0%
gammaln
0.73
23.7%
3562
profile
0.00
0.0%
profreport
0.00
0.0%
Time/call
Self time
Location
3.094000
2.36
76.3%
~/example1.m
0.000206
0.73
23.7%
../toolbox/matlab/specfun/gammaln.m
0.000000
0.00
0.0%
../toolbox/matlab/general/profile.m
0.000000
0.00
0.0%
../toolbox/matlab/general/profreport.m
Code
Calls
Total Time
% Time
4
7
6
result(k) = sin(k/50);
5000
721
5000
2.11
0.84
0.14
3.09
68%
27%
5%
100%
result(k) = gammaln(k);
if result(k) < -0.9
Totals
s
s
s
s
The most time-consuming lines are displayed, along with time, time percentage, and line number. The
most costly lines are the computations on lines 4 and 7.
Another helpful section of the prole report is M-Lint Results, which gives feedback from the M-Lint
code analyzer. Possible errors and suggestions are listed here.
M-Lint results
Line number
Message
4
7
For serious benchmarking, also close your web browser, anti-virus, and other background processes that
may be taking CPU cycles.
Array Preallocation
Matlabs matrix variables have the ability to dynamically augment rows and columns. For example,
>> a = 2
a =
2
>> a(2,6) = 1
a =
2
0
0
0
0
0
0
0
0
0
0
1
Matlab automatically resizes the matrix. Internally, the matrix data memory must be reallocated
with larger size. If a matrix is resized repeatedlylike within a loopthis overhead can be signicant.
To avoid frequent reallocations, preallocate the matrix with the zeros command.
Consider the code:
a(1) = 1;
b(1) = 0;
for k = 2:8000
a(k) = 0.99803 * a(k 1) 0.06279 * b(k 1);
b(k) = 0.06279 * a(k 1) + 0.99803 * b(k 1);
end
This code takes 0.47 seconds to run. After the for loop, both arrays are row vectors of length 8000,
thus to preallocate, create empty a and b row vectors each with 8000 elements.
a = zeros(1,8000);
b = zeros(1,8000);
a(1) = 1;
b(1) = 0;
% Preallocation
for k = 2:8000
a(k) = 0.99803 * a(k 1) 0.06279 * b(k 1);
b(k) = 0.06279 * a(k 1) + 0.99803 * b(k 1);
end
With this modication, the code takes only 0.14 seconds (over three times faster). Preallocation is often
easy to do, in this case it was only necessary to determine the right preallocation size and add two lines.
% Preallocate
for k = 1:10000
v = exp(rand*rand);
if v > 0.5
% Conditionally add to array
count = count + 1;
a(count) = v;
end
end
a = a(1:count);
The average run time of this program is 0.42 seconds without preallocation and 0.18 seconds with it.
Preallocation is also benecial for cell arrays, using the cell command to create a cell array of the
desired size.
JIT Acceleration
Matlab 6.5 (R13) and later feature the Just-In-Time (JIT) Accelerator for improving the speed of
M-functions, particularly with loops. By knowing a few things about the accelerator, you can improve
its performance.
The JIT Accelerator is enabled by default. To disable it, type feature accel off in the console,
and feature accel on to enable it again.
As of Matlab R2008b, only a subset of the Matlab language is supported for acceleration. Upon
encountering an unsupported feature, acceleration processing falls back to non-accelerated evaluation.
Acceleration is most eective when signicant contiguous portions of code are supported.
Data types: Code must use supported data types for acceleration: double (both real and
complex), logical, char, int832, uint832. Some struct, cell, classdef, and function
handle usage is supported. Sparse arrays are not accelerated.
Array shapes: Array shapes of any size with 3 or fewer dimensions are supported. Changing the
shape or data type of an array interrupts acceleration. A few limited situations with 4D arrays
are accelerated.
Function calls: Calls to built-in functions and M-functions are accelerated. Calling MEX functions and Java interrupts acceleration. (See also page 14 on inlining simple functions.)
Conditionals and loops: The conditional statements if, elseif, and simple switch statements
are supported if the conditional expression evaluates to a scalar. Loops of the form for k=a:b,
for k=a:b:c, and while loops are accelerated if all code within the loop is supported.
In-place computation
Introduced in Matlab 7.3 (R2006b), the element-wise operators (+, .*, etc.) and some other functions
can be computed in-place. That is, a computation like
x = 5*sqrt(x.2 + 1);
is handled internally without needing temporary storage for accumulating the result. An M-function
can also be computed in-place if its output argument matches one of the input arguments.
x = myfun(x);
function x = myfun(x)
x = 5*sqrt(x.2 + 1);
return;
To enable in-place computation, the in-place operation must be within an M-function (and for an inplace function, the function itself must be called within an M-function). Currently, there is no support
for in-place computation with MEX-functions.
Multithreaded Computation
Matlab 7.4 (R2007a) introduced multithreaded computation for multicore and multiprocessor computers. Multithreaded computation accelerates some per-element functions when applied to large arrays
(for example, .^, sin, exp) and certain linear algebra functions in the BLAS library. To enable it, select
File Preferences General Multithreading and select Enable multithreaded computation. Further control over parallel computation is possible with the Parallel Computing Toolbox using parfor
and spmd.
JIT-Accelerated Example
For example, the following loop-heavy code is supported for acceleration:
function B = bilateral(A,sd,sr,R)
% The bilateral image denoising filter
B = zeros(size(A));
for i = 1:size(A,1)
for j = 1:size(A,2)
zsum = 0;
for m = R:R
if i+m >= 1 && i+m <= size(A,1)
for n = R:R
if j+n >= 1 && j+n <= size(A,2)
z = exp((A(i+m,j+n) A(i,j))2/(2*sd2))...
* exp((m2 + n2)/(2*sr2));
zsum = zsum + z;
B(i,j) = B(i,j) + z*A(i+m,j+n);
end
end
end
end
B(i,j) = B(i,j)/zsum;
end
end
For a 128 128 input image and R = 3, the run time is 53.3 seconds without acceleration and 0.68
seconds with acceleration.
Vectorization
>> abs([0,1,2,5,6,7])
ans =
1
3
ans =
2
4
% Preallocate
for k = 1:nPoints
% Compute distance for every point
d(k) = sqrt(x(k)2 + y(k)2 + z(k)2);
end
d = min(d);
For every point, its distance from the origin is computed and stored in d. For speed, array d is
preallocated (see Section 3). The minimum distance is then found with min. To vectorize the distance
computation, replace the for loop with vector operations:
function d = minDistance(x,y,z)
% Find the min distance between a set of points and the origin
d = sqrt(x.2 + y.2 + z.2);
d = min(d);
The modied code performs the distance computation with vector operations. The x, y and z arrays
are rst squared using the per-element power operator, .^ (the per-element operators for multiplication
and division are .* and ./). The squared components are added with vector addition. Finally, the
square root of the vector sum is computed per element, yielding an array of distances. (A further
improvement: it is equivalent to compute d = sqrt(min(x.^2 + y.^2 + z.^2))).
The rst version of the minDistance program takes 0.73 seconds on 50000 points. The vectorized
version takes less than 0.04 seconds, more than 18 times faster.
Some useful functions for vectorizing computations:
min, max, repmat, meshgrid, sum, cumsum, diff, prod, cumprod, accumarray
Two arrays are compared per-element. Logic operations return logical arrays with binary values.
How is this useful? Matlab has a few powerful functions for operating on logical arrays:
>> find([1,5,3] < [2,2,4]) % Find indices of nonzero elements
ans =
1
ans =
1
>> all([1,5,3] < [2,2,4])
ans =
0
ans =
1
5
9
By default, find returns element locations as indices (see page 16 for indices vs. subscripts).
10
Alternatively,
i = find(isnan(x) & isinf(x));
x = x(i);
or
x = x(isnan(x) & isinf(x));
sin(x)/x,
1,
x=0
x=0
This code uses find with vectorized computation to handle the two cases separately:
function y = sinc(x)
% Computes the sinc function perelement for a set of x values.
y = ones(size(x));
i = find(x = 0);
y(i) = sin(x(i)) ./ x(i);
11
x =
1
1
1
2
2
2
3
3
3
4
4
4
1
2
3
5
5
5
1
2
3
1
2
3
1
2
3
1
2
3
The matrices above work like a map for a width 5, height 3 image. For each pixel, the x-location can
be read from x and the y-location from y. This may seem like a gratuitous use of memory as x and y
simply record the column and row positions, but this is useful. For example, to draw an ellipse,
% Create x and y for a width 150, height 100 image
[x,y] = meshgrid(1:150,1:100);
% Ellipse with origin (60,50) of size 15 x 40
Img = sqrt(((x60).2 / 152) + ((y50).2 / 402)) > 1;
% Plot the image
imagesc(Img); colormap(copper);
axis image, axis off
Polar functions can be drawn by rst converting x and y variables with the cart2pol function.
[x,y] = meshgrid(1:150,1:100);
[th,r] = cart2pol(x 75,y 50);
% Convert to polar
12
x1 n1
x2 n1
.
n1
xn
x1 n2
x2 n2
x1 2
x2 2
x1
x2
xn n2
xn 2
xn
1
1
.
.
.
1
cn1
cn2
.
.
.
c0
y1
y2
.
.
.
yn
function c = polyint(x,y)
% Given a set of points and function values x and y,
% computes the interpolating polynomial.
x = x(:);
y = y(:);
n = length(x);
The strategy to construct the left-hand side matrix is to rst make two nn matrices of bases and
exponents and then put them together using the per element power operator, .^ . The repmat function
(replicate matrix) is used to make the base matrix xMatrix and the exponent matrix powMatrix.
xMatrix =
x(1) x(1)
x(2) x(2)
.
.
.
x(n) x(n)
x(1)
x(2)
.
.
.
x(n)
powMatrix =
n1 n2
n1 n2
.
.
.
n1 n2
0
0
.
.
.
0
The xMatrix is made by repeating the column vector x over the columns n times. The powMatrix is a
row vector with elements n 1, n 2, n 3, . . . , 0 repeated down the rows n times. The two matrices
could also have been created with [powMatrix, xMatrix] = meshgrid(n-1:-1:0, x).
The code above is only an exampleuse the standard polyfit function for serious use.
13
Every time an M-le function is called, the Matlab interpreter incurs some overhead. Additionally,
many M-le functions begin with conditional code that checks the input arguments for errors or determines the mode of operation.
Of course, this overhead is negligible for a single function call. It should only be considered when the
function being called is an M-le, the function itself is simple, that is, implemented with only a few
lines, and called frequently from within a loop.
For example, this code calls the M-le function median repeatedly to perform median ltering:
% Apply the median filter of size 5 to signal x
y = zeros(size(x)); % Preallocate
for k = 3:length(x)2
y(k) = median(x(k2:k+2));
end
Given a 2500-sample array for x, the overall run time is 0.42 seconds.
Inlining a function means to replace a call to the function with the function code itself. Beware that
inlining should not be confused with Matlabs inline function datatype. Studying median.m (type
edit median on the console) reveals that most of the work is done using the built-in sort function.
The median call can be inlined as
% Apply the median filter of size 5 to signal x
y = zeros(size(x)); % Preallocate
for k = 3:length(x)2
tmp = sort(x(k2:k+2));
y(k) = tmp(3);
% y(k) = median(x(k2:k+2));
end
Now the overall run time is 0.047 seconds, nearly 9 times faster. Furthermore, by inlining median, it
can be specically tailored to evaluating 5-sample medians. But this is only an example; if the Signal
Processing Toolbox is available, y = medfilt1(x,5) is much faster.
A surprising number of Matlabs functions are implemented as M-les, of which many can be inlined
in just a few lines. In Matlab 5.3 (R11), the following functions are worth inlining:
conv, cross, fft2, fliplr, flipud, ifft, ifft2, ifftn, ind2sub, ismember, linspace, logspace,
mean, median, meshgrid, poly, polyval, repmat, roots, rot90, setdiff, setxor, sortrows, std,
sub2ind, union, unique, var
The list above is for Matlab R11; some of these functions have since become built-in. Use the which
command or try edit function name to determine whether a function is implemented as an M-le.
14
For example, in Matlab R11 and earlier, ifft was implemented by simply calling fft and conj. If x
is a one-dimensional array, y = ifft(x) can be inlined with y = conj(fft(conj(x)))/length(x).
Another example: b = unique(a) can be inlined with
b = sort(a(:));
While repmat has the generality to operate on matrices, it is often only necessary to tile a vector or
just a scalar. To repeat a column vector y over the columns n times,
A = y(:,ones(1,n));
% Equivalent to A = repmat(y,1,n);
% Equivalent to A = repmat(x,m,1);
% Equivalent to A = repmat(s,m,n);
This method avoids the overhead of calling an M-le function. It is never slower than repmat (critics
should note that repmat.m itself uses this method to construct mind and nind). For constructing
matrices with constant value, there are other ecient methods, for example, s+zeros(m,n).
Dont go overboard. Inlining functions is only benecial when the function is simple and when it is
called frequently. Doing it unnecessarily obfuscates the code.
15
Referencing Operations
Referencing in Matlab is varied and powerful enough to deserve a section of discussion. Good understanding of referencing enables vectorizing a broader range of programming situations.
1
2
3
.
.
.
10
11 21
12 22
13 23
.
.
.
.
.
.
20 30
81
82
83
.
.
.
90
91
92
93
.
.
.
100
An index refers to an elements position in this one-dimensional array. Using an index, A(83) also refers
to the element on row 3, column 9.
It is faster to scan down columns than over rows. Column-major order means that elements
along a column are sequential in memory while elements along a row are further apart. Scanning down
columns promotes cache eciency.
Conversion between subscripts and indices can be done with the sub2ind and ind2sub functions.
However, because these are M-le functions rather than fast built-in operations, it is more ecient to
compute conversions directly (also see Section 6). For a two-dimensional matrix A of size MN, the
conversions between subscript (i,j) and index (index) are
A(i,j)
A(i + (j-1)*M)
A(index) A(rem(index-1,M)+1, floor((index-1)/M)+1)
Indexing extends to N-D matrices as well, with indices increasing rst through the columns, then
through the rows, through the third dimension, and so on. Subscript notation extends intuitively,
A(. . . , dim4, dim3, row, col).
16
where rowv and colv are vectors. Both may be of any length and their elements may be in any order.
If either is a matrix, it is reshaped to a vector. There is no dierence between using row vectors or
column vectors in vector subscripts.
Let M = length(rowv) and N = length(colv).
side returns a submatrix of size MN:
.
A(rowv(M), colv(1)) A(rowv(M), colv(2))
A(rowv(1), colv(3))
A(rowv(2), colv(3))
A(rowv(M), colv(3))
A(rowv(1), colv(N))
A(rowv(2), colv(N))
.
.
.
A(rowv(M), colv(N))
2
4
left- and right-hand sides (LHS and RHS) refer to the two sides of an assignment, LHS = RHS.
17
refers to all elements of A where the corresponding element of mask is 1 (true). It is equivalent to
A(find(mask)). A common usage of logical indexing is to refer to elements based on a per-element
condition, for example,
A(abs(A) < 1e3) = 0
sets all elements with magnitude less than 103 to zero. Logical indexing is also useful to select nonrectangular sets of elements, for example,
A(logical(eye(size(A))))
references the diagonal elements of A. Note that for a right-hand side reference, it is faster to use
diag(A) to get the diagonal of A.
18
= []
Solving Ax = b
Many problems involve solving a linear system, that is, solving the Ax = b problem
b1
x1
a1,1 a1,2 a1,n
a2,1 a2,2 a2,n x2 b2
.
.
. . = . .
..
.
.
. . .
.
.
.
. . .
bn
xn
an,1 an,2 an,n
The condition number cond(A) describes how sensitive the problem is to small perturbations. Matlab
can eciently approximate cond(A) with condest. Smaller condition number is better, and in this case
A is said to be well-conditioned. An innite condition number implies that A is singularthe problem
cannot be solved, though it may be solved approximately with the pseudoinverse (x = pinv(A)*b).
Small- to moderately-sized problems are solved eciently by the backslash operator:
x = A\b;
% Solves A*x = b
19
Which method works best depends on the particular problem. This diagram (adapted from Demmel [3])
provides a reasonable starting point.
Yes
A denite?
Try pcg
No
No
Yes
Is A wellconditioned?
A symmetric?
No
Is A wellYes conditioned?
available? No
Is storage
expensive?
Try minres
Yes
Yes
Try lsqr
No
Try qmr
Yes
No
Try gmres
Functional Representation
Rather than storing the matrix explicitly, it is often convenient to represent A as a linear function
acting on x. For example,
1
x1
1 1
x2
. .
A*x = .
..
.
.
.
.
.
1 1 1 xn
can be expressed as A*x = cumsum(x). In addition to being conceptually convenient, functional representation avoids the memory overhead of storing the matrix.
All the iterative solvers support functional representation, where a function handle afun is given in place
of matrix A. The function afun(x) should accept a vector x and return A*x. The help for lsqr (help
lsqr) has a functional example where A is the central dierencing operator. The bicg, lsqr, and qmr
methods also require the transpose: afun(x,notransp) should return A*x and afun(x,transp)
should return A*x. Similarly, the preconditioner may be given as a function handle mfun.
Special Matrices
For certain special matrix structures, the Ax = b problem can be solved very quickly.
Circulant matrices. Matrices corresponding to cyclic convolution Ax = h x are diagonalized in
the Fourier domain, and can be solved by x = ifft(fft(b)./fft(h));
Triangular and banded. Triangular matrices and diagonally-dominant banded matrices are solved
eciently by sparse LU factorization: [L,U] = lu(sparse(A)); x = U\(L\b);
Poisson problem. If A is a nite dierence approximation of the Laplacian, the problem is eciently solved by multigrid methods (e.g., web search for matlab multigrid).
20
solve Ax = b %%
% Convergence tolerance
% Maximum number of iterations
% Set x to initial guess (if no guess is available, use x0 = 0)
Compute w = A x
r = b w;
% r = residual vector
Compute w = A p
alpha = e/(p'*w);
x = x + alpha*p;
r = r alpha*w;
After the loop, the nal solution is in x. The method converged successfully with accuracy tol if it
encountered a break statement, otherwise, it failed or needed more iterations. The relative residual
may be computed by relres = norm(r)/norm(b) after the loop and resvec by storing the value of
norm(r) each iteration.
21
Numerical Integration
f (x) dx
a
wk f (xk ),
k
where the xk are called the nodes or abscissas and wk are the associated weights. Simpsons rule is
b
f (x) dx
a
h
[f (a) + 4f (a + h) + f (b)] ,
3
h=
ba
.
2
h 4h h
3, 3 , 3.
adaptive
adaptive
adaptive
adaptive
Simpson
Gauss-Lobatto
Gauss-Kronrod
Simpson
The quad- functions are robust and precise, however, they are not very ecient. They use an adaptive renement procedure, and while this reduces the number of function calls, they gain little from
vectorization and incur signicant overhead.
If an application requires approximating an integral whose integrand can be eciently vectorized, using
nonadaptive quadrature may improve speed.
for n = 20:20 % Compute Fourier series coefficients
c(n + 21) = quad(@(x)(exp(sin(x).6).*exp(1i*x*n)),0,pi,1e4);
end
This code runs in 5.16 seconds. In place of quad, using Simpsons composite rule with N = 199 nodes
yields results with comparable accuracy and allows for vectorized computation. Since the integrals are
all over the same interval, the nodes and weights only need to be constructed once.
N = 199; h = pi/(N1);
x = (0:h:pi).';
w = ones(1,N); w(2:2:N1) = 4;
w(3:2:N2) = 2;
w = w*h/3;
% Nodes
% Weights
for n = 20:20
c(n + 21) = w * ( exp(sin(x).6).*exp(1i*x*n) );
end
This version of the code runs in 0.02 seconds (200 times faster). The quadrature is performed by the
dot product multiplication with w. It can be further optimized by replacing the for loop with one
vector-matrix multiply:
[n,x] = meshgrid(20:20, 0:h:pi);
c = w * ( exp(sin(x).6).*exp(1i*x.*n) );
22
For this example, quadv can be used on a multi-valued integrand with similar accuracy and speed,
n = 20:20;
c = quadv(@(x)exp(sin(x).6).*exp(1i.*x.*n),0,pi,1e4);
h
x
w
I
(b a)/(N1);
(a:h:b).';
ones(1,N); w(2:2:N1) = 4; w(3:2:N2) = 2; w = w*h/3;
w * f(x);
% Approximately evaluate the integral
N
h
d
x
w
I
=
=
=
=
=
=
1
1
1
f (x) dx 6 f (1) + 5 f ( 5 ) + 5 f ( 5 ) + 1 f (1).
6
6
6
The number of nodes N must be such that (N 1)/3 is an integer. If not, the rst line adjusts N to
the closest valid choice. It is usually more accurate than Simpsons rule when f has six continuous
derivatives, f C 6 (a, b).
A disadvantage of this nonadaptive approach is that the accuracy of the result is only indirectly controlled by the parameter N. To guarantee a desired accuracy, either use a generously large value for N
or, if possible, determine the error bounds [6, 7]
Simpsons rule error
(b a)h4
max f (4) () provided f C 4 (a, b),
ab
180
27(b a)h6
max f (6) () provided f C 6 (a, b),
ab
56000
ba
where h = N 1 . Note that these bounds are valid only when the integrand is suciently dierentiable: f
must have four continuous derivatives for the Simpsons rule error bound, and six continuous derivatives
for 4th-order Gauss-Lobatto.
23
Composite Simpsons rule is a fast and eective default choice. But depending on the situation, other
methods may be faster and more accurate:
If the integrand is expensive to evaluate, or if high accuracy is required and the error bounds
above are too dicult to compute, use one of Matlabs adaptive methods. Otherwise, consider
composite methods.
Use higher-order methods (like Gauss-Lobatto/quadl) for very smooth integrands and lower-order
methods for less smooth integrands.
Use the substitution u =
1
1x
.
0
An approach for evaluating double integrals of the form a c f (x, y) dy dx is to apply one-dimensional
b
quadrature to the outer integral a F (x) dx and then for each x use one-dimensional quadrature over
d
the inner dimension to approximate F (x) = c f (x, y) dy. The following code does this with composite
Simpsons rule with NxNy nodes:
%%% Construct Simpson nodes and weights over x %%%
h = (b a)/(Nx1);
x = (a:h:b).';
wx = ones(1,Nx); wx(2:2:Nx1) = 4; wx(3:2:Nx2) = 2;
wx = w*h/3;
wy = w*h/3;
x = x(:); y = y(:);
w = w(:).';
z = z(:);
When the integration region is complicated or of high dimension, Monte Carlo integration techniques
are appropriate. The disadvantage is that an N -point Monte Carlo quadrature has error on the
order O( 1 ), so many points are necessary even for moderate accuracy. Suppose that N points,
N
x1 , x2 , . . . , xN , are uniformly randomly selected in a multidimensional region (or volume) . Then
f dV
dV
N
24
f (xn ).
n=1
To integrate a complicated region W that is dicult to sample uniformly, nd an easier region that
contains W and can be sampled [5]. Then
f W dV
f dV =
W
dV
N
f (xn )W (xn ),
W (x) =
n=1
1,
0,
x W,
x W.
/
W (x) is the indicator function of W : W (x) = 1 when x is within region W and W (x) = 0 otherwise.
Multiplying the integrand by W sets contributions from outside of W to zero.
For example, consider nding the center of mass of the shape W dened by cos 2
2
x2 + y 2 x y
and x + y 4. Given the integrals M = W dA, Mx = W x dA, and My = W y dA, the center of
M
mass is ( Mx , My ). The region is contained in the rectangle dened by 2 x 2 and 2 y 2.
M
The following code estimates M , Mx , and My :
%%% Uniformly randomly sample points (x,y) in %%%
x = 4*rand(N,1) 2;
y = 4*rand(N,1) 2;
-2
-2
=
=
=
=
a + (ba)*rand(N,1);
c + (dc)*rand(N,1);
logical(indicatorW(x,y));
x(i); y = y(i);
area = (ba)*(dc);
I = (area/N) * sum(f(x,y));
25
10
Signal Processing
Even without the Signal Processing Toolbox, Matlab is quite capable in signal processing computations. This section lists code snippets to perform several common operations eciently.
Moving-average lter
To compute an N-sample moving average of x with zero padding:
y = filter(ones(N,1)/N,1,x);
% Linear interpolation
Since local maximum and minimum points of a signal have zero derivative, their locations can be
estimated from the zero-crossings of diff(x), provided the signal is sampled with suciently ne
resolution. For a coarsely sampled signal, a better estimate is
iMax = find(sign(x(2:end1)x(1:end2))
+ sign(x(2:end1)x(3:end)) >
iMin = find(sign(x(2:end1)x(1:end2))
+ sign(x(2:end1)x(3:end)) <
...
0) + 1;
...
0) + 1;
FFT-based convolution
This line performs FFT-based circular convolution, equivalent to y = filter(b,1,x) except near the
boundaries, provided that length(b) < length(x):
y = ifft(fft(b,length(x)).*fft(x));
In both code snippets above, y will be complex-valued, even if x and b are both real. To force y to be
real, follow the computation with y = real(y). If you have the Signal Processing Toolbox, it is faster
to use fftfilt for FFT-based, zero-padded ltering.
26
Similarly, given a boundary extension value padRight for x(end+1), the lter yn = b1 xn+1 + b2 xn is
implemented as
y = filter(b,1,[x(2:end),padRight],x(1)*b(2));
Choices for padLeft and padRight for various boundary extensions are
Boundary extension
Periodic
Whole-sample symmetric
Half-sample symmetric
Antisymmetric
padLeft
x(end)
x(2)
x(1)
2*x(1)-x(2)
padRight
x(1)
x(end-1)
x(end)
2*x(end)-x(end-1)
It is in principle possible to use a similar approach for longer lters, but ironically, computing the initial
condition itself requires ltering. To implement noncausal ltering and ltering with other boundary
handling, it is usually fastest to pad the input signal, apply filter, and then truncate the result.
27
Haar Wavelet
This code performs K stages of the Haar wavelet transform on the input data x to produce wavelet
coecients y. The input array x must have length divisible by 2K .
Inverse transform:
Forward transform:
y = x(:); N = length(y);
x = y(:); N = length(x)*pow2(K);
for k = 1:K
tmp = y(1:2:N) + y(2:2:N);
y([1:N/2,N/2+1:N]) = ...
[tmp;y(1:2:N) 0.5*tmp]/sqrt(2);
N = N/2;
end
for k = 1:K
N = N*2;
tmp = x(N/2+1:N) + 0.5*x(1:N/2);
x([1:2:N,2:2:N]) = ...
[tmp;x(1:N/2) tmp]*sqrt(2);
end
Inverse transform:
U = 0.25*[2sqrt(3),sqrt(3)];
ScaleS = (sqrt(3) 1)/sqrt(2);
ScaleD = (sqrt(3) + 1)/sqrt(2);
U = 0.25*[2sqrt(3),sqrt(3)];
ScaleS = (sqrt(3) 1)/sqrt(2);
ScaleD = (sqrt(3) + 1)/sqrt(2);
y = x(:); N = length(y);
x = y(:); N = length(x)*pow2(K);
for k = 1:K
N = N/2;
y1 = y(1:2:2*N);
y2 = y(2:2:2*N) + sqrt(3)*y1;
y1 = y1 + filter(U,1,...
y2([2:N,max(N1,1)]),y2(1)*U(2));
y(1:2*N) = [ScaleS*...
(y2 y1([min(2,N),1:N1]));ScaleD*y1];
end
for k
y1
y2
y1
= 1:K
= x(N+1:2*N)/ScaleD;
= x(1:N)/ScaleS + y1([min(2,N),1:N1]);
= y1 filter(U,1,...
y2([2:N,max(N1,1)]),y2(1)*U(2));
x([1:2:2*N,2:2:2*N]) = [y1;y2 sqrt(3)*y1];
N = 2*N;
end
To use periodic boundary handling rather than symmetric boundary handling, change appearances of
[2:N,max(N-1,1)] to [2:N,1] and [min(2,N),1:N-1] to [N,1:N-1].
28
11
Miscellaneous
lowerBound
lowerBound;
x > upperBound
upperBound;
Unfortunately, this is slow. A faster method is to use the min and max functions:
x = max(x,lowerBound);
x = min(x,upperBound);
Similarly, x = x(:,:) reduces an N-d array to 2D, where the number of rows stays the same.
29
Flood lling
Flood lling, like the bucket tool in image editors, can be elegantly written as a recursive function:
function I = flood1(I,c,x,y)
% Flood fills image I from point (x,y) with color c.
c2 = I(y,x);
I(y,x) = c;
if
if
if
if
x
x
y
y
>
<
>
<
1
size(I,2)
1
size(I,1)
&
&
&
&
I(y,x1)
I(y,x+1)
I(y1,x)
I(y+1,x)
==
==
==
==
c2
c2
c2
c2
&
&
&
&
I(y,x1)
I(y,x+1)
I(y1,x)
I(y+1,x)
=
=
=
=
c,
c,
c,
c,
I
I
I
I
=
=
=
=
flood1(I,c,x1,y);
flood1(I,c,x+1,y);
flood1(I,c,x,y1);
flood1(I,c,x,y+1);
end
end
end
end
Being a highly recursive function, this is inecient in Matlab. The following code is faster:
function I = flood2(I,c,x,y)
% Flood fills image I from point (x,y) with color c.
LastFlood = zeros(size(I));
Flood = LastFlood;
Flood(y,x) = 1;
Mask = (I == I(y,x));
FloodFilter = [0,1,0; 1,1,1; 0,1,0];
while any(LastFlood(:) = Flood(:))
LastFlood = Flood;
Flood = conv2(Flood,FloodFilter,'same') & Mask;
end
I(find(Flood)) = c;
The key is the conv2 two-dimensional convolution function. Flood lling a 4040-pixel region takes
1.168 seconds with flood1 and 0.067 seconds with flood2.
30
12
Further Reading
MEX
In a coding situation that cannot be optimized any further, keep in mind that the Matlab language
is intended primarily for easy prototyping rather than high-speed computation. In some cases, an
appropriate solution is Matlab Executable (MEX) external interface functions. With a C or Fortran
compiler, it is possible to produce a MEX function that can be called from within Matlab in the same
as an M-function. The typical speed improvement over equivalent M-code is easily ten-fold.
Installations of Matlab include an example of MEX under directory <MATLAB>/extern/examples/mex.
In this directory, a function yprime is written as M-code (yprime.m) and equivalent C (yprime.c)
and Fortran (yprime.f) MEX source code. For more information, see the MathWorks MEX-les guide
http://www.mathworks.com/support/tech-notes/1600/1605.html
For information on compiling MEX les with the GNU compiler collection (GCC), see
http://gnumex.sourceforge.net
Beware, MEX is an ambitious alternative to M-code. Writing MEX les requires learning some low-level
details about Matlab, and takes more time to develop than M-code.
31
Matlab Compiler
The Matlab Compiler generates a standalone executable from M-code. For product information and
documentation, see
http://www.mathworks.com/access/helpdesk/help/toolbox/compiler
References
[1] A. Bielajew. The Fundamentals of the Monte Carlo Method for Neutral and Charged Particle
Transport. University of Michigan, class notes. Available online at
http://www-personal.engin.umich.edu/~bielajew/MCBook/book.pdf
[2] I. Daubechies and W. Sweldens. Factoring Wavelet Transforms into Lifting Steps. Sep. 1996.
[3] J. W. Demmel. Applied Numerical Linear Algebra. SIAM, 1997.
[4] W. Gander and W. Gautschi. Adaptive QuadratureRevisited, BIT, vol. 40, pp. 84-101, 2000.
[5] W. Press, B. Flannery, S. Teukolsky and W. Vetterling. Numerical Recipes. Cambridge
University Press, 1986.
[6] E. Weisstein. Lobatto Quadrature. From MathWorldA Wolfram Web Resource.
http://mathworld.wolfram.com/LobattoQuadrature.html
[7] E. Weisstein. Simpsons Rule. From MathWorldA Wolfram Web Resource.
http://mathworld.wolfram.com/SimpsonsRule.html
32