Fast Matlab Code
Fast Matlab Code
Contents
1 The Proler 2 Array Preallocation 3 Vectorization 3.1 Vectorized Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Vectorized Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Inlining Simple Functions 5 Integration 5.1 One-Dimensional Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Multidimensional Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Referencing Operations 6.1 Subscripts vs. Indices . . 6.2 Vectorized Subscripts . . . 6.3 Vector Indices . . . . . . . 6.4 Reference Wildcards . . . 6.5 Deleting Submatrices with 2 3 5 5 6 10 12 13 14 16 16 16 17 18 18 18 18 19 19 20 20 21
. . . . . . . . []
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
7 Miscellaneous Tricks 7.1 Bound a value without if statements . . . . 7.2 Convert any array into a column vector . . 7.3 Flood lling . . . . . . . . . . . . . . . . . . 7.4 Find the min/max of a matrix or N-d array 7.5 Vectorized use of set on GUI objects . . . . 8 Going Further
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
Introduction
The Matlab programming language is parsed, code is interpreted in realtime. Languages like C++ and Fortran are faster because they are compiled ahead of time into the computers native language. The advantages of parsing in realtime are greater platform independence, robustness, and easier debugging. The disadvantages are a signicant loss in speed, increased overhead, and limited low-level control. To compensate the speed loss, Matlab oers means to help speed up code. This article discusses these and other strategies to improving the speed of Matlab code.
Array preallocation Vectorization Inlining simple functions
Keep in mind that Matlab has gone through many versions and that it is available on many platforms. Hence the fastest method on one system may not be the fastest on another. This article provides methods that are generally fast, but makes no claim on what is the fastest.
Caution!
Learn the language rst: Optimization requires comfort with the syntax and functions of the language. This article is not a tutorial on MATLAB. Use comments: Optimized code tends to be terse and cryptic. Help others and yourself by remembering to comment. Dont optimize code before its time: Before ever optimizing code, consider if it will be worth the eort. If the code will soon be revised or extended, it will be rewritten anyway. Only optimize where necessary: Make sure the code is really a speed bottleneck. If it isnt, optimization only obfuscates the code.
The Proler
Matlab 5.0 and newer versions include a tool called the proler that helps determine where the bottlenecks are in a program. Consider the following function: .....................................................................................................
function result = example1(Count) for k = 1:Count result(k) = sin(k/50); if result(k) < 0.9 result(k) = gammaln(k); end end
..................................................................................................... To analyze the eciency this function, rst enable the proler and clear any old proler data:
>> profile on >> profile clear
Now run the program. Change the input argument higher or lower so that it takes about a second.
>> example1(5000);
Then enter
>> profreport('example1')
The proler generates an HTML report on the function and launches a browser window. Depending on the system, proler results may be a little dierent from this example.
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.
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 for loopthis overhead becomes noticeable. 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
..................................................................................................... The proler timed this code to take 0.47 seconds. 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. What if the nal array size can vary? One approach is to use the upper bound on the array size and cut the excess after the loop: .....................................................................................................
a = zeros(1,10000); count = 0; % Preallocate
for k = 1:10000 v = exp(rand(1)*rand(1)); if v > 0.5 % Conditionally add to array count = count + 1; a(count) = v; end end a = a(1:count); % Trim the result
..................................................................................................... The average run time of this program is 0.42 seconds without preallocation and 0.18 seconds with it. Preallocation can also be done with cell arrays, using the cell command to create the desired size. Using preallocation with a frequently resizing cell array is even more benecial than with double arrays.
Vectorization
To vectorize a computation means to replace parallel operations with vector operations. This strategy often improves speed ten-fold. Good vectorization is a skill that must be developed; it requires comfort with Matlabs language and creativity.
3.1
Vectorized Computations
Many standard Matlab functions are vectorized, they can operate on an array as if the function had been applied individually to every element.
>> sqrt([1,4;9,16]) ans = 1 3 2 4 >> abs([0,1,2,5,6,7]) ans = 0 1 2 5 6 7
% 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); % Get the minimum distance
..................................................................................................... For every point, the distance between it and the origin is computed and stored in d. 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); % Compute distance for every point % Get the minimum distance
..................................................................................................... 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. 5
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, filter
3.2
Vectorized Logic
The previous section shows how to vectorize pure computation. Bottleneck code often involves conditional logic. Like computations, Matlabs logic operators are vectorized:
>> [1,5,3] < [2,2,4] ans = 1 0 1
Two arrays are compared per-element. Logic operations return logical arrays with binary elements. How is this useful? Matlab has a few powerful functions for operating on logical arrays:
find: Find indices of nonzero elements. any: True if any element of a vector is nonzero (or per-column for a matrix). all: True if all elements of a vector are nonzero (or per-column for a matrix).
The find function returns the indices where the vector logic operation returns true. In the rst example, 1 < 2 is true, 5 < 2 is false, and 3 < 4 is true, so find reports that the rst and third comparisons are true. In the second example, find returns the indices where the identity matrix is equal to one. The indices 1, 5, and 9 correspond to the diagonal of a 3 by 3 matrix. The any and all functions are simple but occasionally very useful. For example, any(x(:) returns true if any element of x is negative. < 0)
Alternatively,
i = find(isnan(x) & isinf(x)); x = x(i); % Find elements that are not NaN and not infinite % Keep those elements
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); % Set y to all ones, sinc(0) = 1 % Find nonzero x values % Compute sinc where x = 0
.....................................................................................................
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); % Spiral centered at (75,50) Img = sin(r/3 + th); imagesc(Img); colormap(hot); axis image; axis off;
% Convert to polar
.....................................................................................................
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); %%% Construct the lefthand side matrix xMatrix = repmat(x, 1, n); powMatrix = repmat(n1:1:0, n, 1); A = xMatrix . powMatrix; c = A \ y; % Make sure x and y are both column vectors % n = Number of points %%% % Make an n by n matrix with x on every column % Make another n by n matrix of exponents % Compute the powers % Solve matrix equation for coefficients
..................................................................................................... The strategy to construct the left-hand side matrix is to rst make two n by n 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. Similarly, 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). This function is only an example; use the standard polyfit function for serious polynomial interpolation. It is more general and algorithmically more ecient (see polyfit.m).
Every time an M-le function is called, Matlab incurs some overhead to nd and parse the le and to create a local workspace for the functions local variables. 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 is simple, that is, implemented with only a few lines, and it is called frequently within a loop. For example, this code calls the M-le function median repeatedly from within a for loop: .....................................................................................................
% 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. Thus the median call can be inlined: .....................................................................................................
% 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 for a 2500-sample input is 0.047 seconds, nearly 9 times faster. Notice that by inlining median, it can be specically tailored to evaluating 5-sample medians. (This is only an example of inlining; if the Signal Processing Toolbox is available, y = medfilt1(x,5) is faster.) A surprising number of Matlabs functions are implemented as M-les, of which many can be inlined in just a few lines. If called repeatedly, the following functions are worthwhile inlining:
linspace, logspace mean, median, std, var
10
ifft, fft2, ifft2, ifftn, conv fliplr, flipud, meshgrid, repmat, rot90, sortrows ismember, setdiff, setxor, union, unique poly, polyval, roots sub2ind, ind2sub
(To view the code for linspace, type edit linspace on the console). For example, ifft is implemented by simply calling fft and conj (see ifft.m). 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(:)); b(find(b((1:end1)')==b((2:end)'))) = [];
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);
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). Warning: 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.
11
Integration
b
Numerical integration is usually done with quadrature formulas, which have the general form f (x) dx
a k
wk f (xk ),
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.
Matlab oers quad and quadl for quadrature in one dimension. These functions are robust and precise, however, they are not very ecient. Both use an adaptive renement procedure to reduce the number of function calls. However, as quad and quadl are recursive M-le functions, the algorithmic overhead is signicant. Furthermore, adaptive renement gains little from vectorization. If an application requires approximating dozens of integrals, and if the integrand function can be eciently vectorized, using nonadaptive quadrature may improve speed. .....................................................................................................
% Approximate Fourier series coefficients for exp(sin(x)6) for frequencies 20 to 20 for n = 20:20 c(n + 21) = quad(inline('exp(sin(x)6)*exp(i*x*n)','x','n'),0,pi,1e4,[],n); 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 enables vectorized computation. Since the integrals are all over the same interval, the nodes and weights need only be constructed once. .....................................................................................................
N = 199; h = pi/(N1); x = (0:h:pi).'; w = ones(1,N); w(2:2:N1) = 4; % Nodes % Weights
w(3:2:N2) = 2;
w = w*h/3;
..................................................................................................... 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(i*x.*n) );
12
5.1
b a
One-Dimensional Integration
h x w I
where N is an odd integer specifying the number of nodes. A good higher-order choice is composite 4th -order Gauss-Lobatto [2], based on the approximation
1 1 1 5 1 1 1 f (1) + 5 f (x) dx 6 6 f ( 5 ) + 6 f ( 5 ) + 6 f (1).
N h d x w I
= = = = = =
max(3*round((N1)/3),3) + 1; % Adjust N to the closest valid choice (b a)/(N1); (3/sqrt(5) 1)*h/2; (a:h:b).'; x(2:3:N2) = x(2:3:N2) d; x(3:3:N1) = x(3:3:N1) + d; ones(1,N); w(4:3:N3) = 2; w([2:3:N2,3:3:N1]) = 5; w = w*h/4; % Approximately evaluate the integral w * f(x);
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 [4, 5] Simpsons rule error 4 th -order Gauss-Lobatto error (b a)h4 max f (4) ( ) provided f C 4 (a, b), a b 180 27(b a)h6 max f (6) ( ) provided f C 6 (a, b), a b 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 Gauss-Lobatto.
For most purposes, composite Simpsons rule is a sucient default choice. Depending on the integrand, other choices can improve accuracy:
Use higher-order quadrature formulas if the integrand has many continuous derivatives. Use lower-order if the integrand function is not smooth. Use the substitution u =
1 1x
. 0
13
5.2
Multidimensional Integration
b d
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; %%% Construct Simpson nodes and weights over y %%% h = (d c)/(Ny1); y = (c:h:d).'; wy = ones(1,Ny); wy(2:2:Ny1) = 4; wy(3:2:Ny2) = 2; %%% Combine for twodimensional integration %%% [x,y] = meshgrid(x,y); x = x(:); y = y(:); w = wy.'*wx; w = w(:).'; I = w * f(x,y); % Approximately evaluate the integral
wx = w*h/3;
wy = w*h/3;
When the integration region is complicated or of high dimension, Monte Carlo integration techniques are appropriate. The disadvantage with these statistical approaches is that accuracy increases with only the square root of the number of points. Nevertheless, the basic Monte Carlo idea is straightforward and of practical value. Suppose that N points, x1 , x2 , . . . , xN , are uniformly randomly selected in a multidimensional volume . Then f dV
dV N
f (xn ).
n=1
To integrate a complicated volume W that is dicult to sample uniformly, nd an easier volume that contains W and can be sampled [3]. Then f dV =
W
f W dV
dV N
f (xn )W (xn ),
n=1
W (x) =
1, 0,
x W, x / W.
W (x) is the indicator function of W : W (x) = 1 when x is within volume 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 and x2 + y 2 4. Given the integrals M =
W
x2 + y 2 x y
dA, Mx = 14
x dA, and My =
y x mass is ( M M , M ). The region is contained in the rectangle dened by 2 x 2 and 2 y 2. The following code estimates M , Mx , and My :
%%% Uniformly randomly sample points (x,y) in Omega %%% x = 4*rand(N,1) 2; y = 4*rand(N,1) 2; %%% Restrict the points to region W %%% i = find(cos(2*sqrt(x.2 + y.2)).*x <= y & x.2 + y.2 <= 4); x = x(i); y = y(i); %%% Approximately evaluate the integrals %%% area = 4*4; % The area of rectangle Omega M = (area/N) * length(x); Mx = (area/N) * sum(x); My = (area/N) * sum(y);
Region W sampled with N = 104 . The center of mass (the red X) is approximately (0.47, 0.67).
More generally, if W is a two-dimensional region contained in the rectangle dened by a x b and c y d, the following code approximates W f dA:
x y i x = = = = a + (ba)*rand(N,1); c + (dc)*rand(N,1); find(indicatorW(x,y)); x(i); y = y(i);
where indicatorW(x,y) is the indicator function W (x, y ) for region W . Monte Carlo integration is a study of its own, see for example [1] for renements and variations.
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.
6.1
Subscripts are the most common method used to refer to matrix elements, for example, A(3,9) refers to row 3, column 9. Indices are an alternative referencing method. Consider a 10 10 matrix A. Internally, Matlab stores the matrix data linearly as a one-dimensional, 100-element array of data. 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. For example, A(83) refers to the element on row 3, column 9. 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 much more ecient to compute conversions directly. For a two-dimensional matrix A of size M by N, the conversion between subscript (i,j) and index (index): A(i,j) A(i + (j-1)*M) A(index) A(rem(index-1,M)+1, floor(index/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).
6.2
Vectorized Subscripts
It is useful to work with submatrices rather than individual elements. This is done with a vector of indices or subscripts. If A is a two-dimensional matrix, a vector subscript reference has the syntax A(rowv, colv) where rowv is a vector of rows with M elements and colv is a vector of columns with N elements. 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. On the right-hand side (rhs) of an operation, a vector subscript reference returns a submatrix of elements of size MN:
16
A(rowv(1), colv(1)) A(rowv(1), colv(2)) A(rowv(1), colv(3)) A(rowv(2), colv(1)) A(rowv(2), colv(2)) A(rowv(2), colv(3)) . . . A(rowv(M), colv(1)) A(rowv(M), colv(2)) A(rowv(M), colv(3))
If the vector subscripted matrix is on the left-hand side (lhs), the rhs result must MN or scalar size. If any elements in the destination reference are repeated, for example, this ambiguous assignment of A(1,2) and A(2,2), A([1,2],[2,2]) = [1,2;3,4] A(1, 2) A(1, 2) A(2, 2) A(2, 2) = 1 3 2 4
it is the value in the source array with the greater index that dominates.
>> A = zeros(2); A([1,2],[2,2]) = [1,2;3,4] A = 0 0 2 4
6.3
Vector Indices
Multiple elements can also be referenced with vector indices. A(indexv) where indexv is an array of indices. On the rhs, a vector index reference returns a matrix the same size as indexv. For example, if indexv is a 3 4 matrix, A(indexv) is the 3 4 matrix A(indexv(1, 1)) A(indexv(1, 2)) A(indexv(1, 3)) A(indexv(1, 4)) A(indexv) = A(indexv(2, 1)) A(indexv(2, 2)) A(indexv(2, 3)) A(indexv(2, 4)) A(indexv(3, 1)) A(indexv(3, 2)) A(indexv(3, 3)) A(indexv(3, 4)) While vector subscripts are limited to referring to block-shaped submatrices, vector indices can refer to any shape. If a vector index reference is on the lhs, the rhs must return a matrix of the same size as indexv or a scalar. As with vector subscripts, ambiguous duplicate assignments use the later value assigned.
>> A = zeros(2); A = 0 0 3 4 A([3,4,3,4]) = [1,2,3,4]
17
6.4
Reference Wildcards
Using the wildcard, :, in a subscript refers to an entire row or column. For example, A(:,1) refers to every row in column onethe entire rst column. This can be combined with vector subscripts, A([2,4],:) refers to the second and fourth rows. When the wildcard is used in a vector index, the entire matrix is referenced. On the rhs, this always returns a column vector. A(:) = column vector This is frequently useful: for example, if a function input must be a row vector, the users input can be quickly reshaped into row vector with A(:). (make a column vector and transpose to a row vector). A(:) on the lhs assigns all the elements of A, but does not change its size. For example, A(:) = 8 changes all elements of matrix A to 8.
6.5
Elements in a matrix can be deleted by assigning the empty matrix. For example, A([3,5]) = [ ] deletes the third and fth element from A. If this is done with index references, the matrix is reshaped into a row vector. It is also possible to delete with subscripts if all but one subscript are the wildcard. A(2,:) deletes the second row. Deletions like A(2,1) = [ ] or even A(2,1:end) = [ ] are illegal. = []
7
7.1
Miscellaneous Tricks
Bound a value without if statements
Unfortunately, this is slow. A faster method is to use the min and max functions:
x = max(x,lowerBound); x = min(x,upperBound); % Bound elements from below, x >= lowerBound % Bound elements from above, x <= upperBound
18
7.2
It is often useful to force an array to be a column vector, for example, when writing a function expecting a column vector as an input. This simple trick will convert the input array to a column vector if the array is a row vector, a matrix, an N-d array, or already a column vector.
x = x(:); % convert x to a column vector
By following this operation with a transpose ., it is possible to convert an array to a row vector.
7.3
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 40 40-pixel region takes 1.168 seconds with flood1 and 0.067 seconds with flood2.
19
7.4
Given a matrix input (with no other inputs), the min and max functions operate along the columns, nding the extreme element in each column. Often it is more useful to nd the extreme element of the entire matrix. It is possible to repeat the min or max function on the column extremes; min(min(A)) to nd the minimum of a two-dimensional matrix A. This method uses only one call to min/max (using the convert to column vector trick) and determines the extreme elements location:
[MinValue, MinIndex] = min(A(:)); % Find the minimum element in A % The minimum value is MinValue, the index is MinIndex MinSub = ind2sub(size(A), MinIndex); % Convert MinIndex to subscripts
The minimum element is A(MinIndex) or A(MinSub(1), MinSub(2), ...) as a subscript reference. (Similarly, replace min with max for maximum value.)
7.5
A serious graphical user interface (GUI) can have dozens of objects like text labels, buttons, and sliders. These objects must all be initialized with the uicontrol function with lengthy property names. For example, to dene three edit boxes with white text background and left text alignment:
uicontrol('Units', 'normalized', 'Position', [0.1,0.9,0.7,0.05], ... 'HorizontalAlignment', 'left', 'Style', 'edit', 'BackgroundColor', [1,1,1]); uicontrol('Units', 'normalized', 'Position', [0.1,0.8,0.7,0.05], ... 'HorizontalAlignment', 'left', 'Style', 'edit', 'BackgroundColor', [1,1,1]); uicontrol('Units', 'normalized', 'Position', [0.1,0.7,0.7,0.05], ... 'HorizontalAlignment', 'left', 'Style', 'edit', 'BackgroundColor', [1,1,1]);
This is excessive for just three edit boxes. A vectorized call to set can reduce the wordiness:
h(1) = uicontrol('Units', 'normalized', 'Position', [0.1,0.9,0.7,0.05]); h(2) = uicontrol('Units', 'normalized', 'Position', [0.1,0.8,0.7,0.05]); h(3) = uicontrol('Units', 'normalized', 'Position', [0.1,0.7,0.7,0.05]); set(h, 'HorizontalAlignment', 'left', 'Style', 'edit','BackgroundColor', [1,1,1]);
20
Going Further
In a coding situation that cannot be optimized any further, keep in mind that Matlab is intended as a prototyping language. In some cases, an appropriate solution is the use of MEX-les (Matlab EXternal interface les). With a C or Fortran compiler, it is possible to produce fast functions that interface with Matlab. The speed improvement over the equivalent m-le program can easily be ten-fold. See the MathWorks MEX-les Guide http://www.mathworks.com/support/tech-notes/1600/1605.html Warning: Writing MEX-les requires solid understanding of Matlab and comfort with C or Fortran, and typically more time to develop than M code. Nevertheless, it is a possibility for improving speed. Good luck and happy coding.
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] W. Gander and W. Gautschi. Adaptive QuadratureRevisited, BIT, vol. 40, pp. 84-101, 2000. [3] W. Press, B. Flannery, S. Teukolsky and W. Vetterling. Numerical Recipies. Cambridge University Press, 1986. [4] E. Weisstein. Lobatto Quadrature. From MathWorldA Wolfram Web Resource. http://mathworld.wolfram.com/LobattoQuadrature.html [5] E. Weisstein. Simpsons Rule. From MathWorldA Wolfram Web Resource. http://mathworld.wolfram.com/SimpsonsRule.html
21