I've been asked repeatedly about the performance comparison between two MATLAB functions, bsxfun and repmat. These two functions can each help with calculations in which two arrays are expected to have the same dimensions, but some of the input dimensions, instead of agreeing, may have the value 1. The simple example I use here is subtracting the columns means from a matrix.
First I set up the data.
m = 1e5; n = 100; A = rand(m,n);
And now here's the code I'm tempted to write, safely tucked inside the confines of a try statement.
try AZeroMean = A - mean(A); catch ME disp(ME.message); end
Matrix dimensions must agree.
As you can see, MATLAB does not allow binary operators to work on arrays with different sizes (except when one of the inputs is a scalar value). There are at least two ways to remedy this.
Using the most excellent timeit utility that Steve Eddins posted to the file exchange, I now time the repmat calculations. First I create an anonymous function that does my calculation. Then I pass that function handle to timeit. timeit carefully warms up the function by running it enough so the times are not subject to first-time effects, figuring out how many times to run it to get meaningful results, and more.
frepmat = @() A - repmat(mean(A),size(A,1),1); timeit(frepmat)
ans = 0.30964
repmat uses a variety of techniques for replicating an array, depending on the details of what's being replicated. One technique is to index into the array with ones in the dimension to replicate. Here's an illustative example with a vector.
q = [17 pi 42 exp(1)]; q5 = repmat(q,5,1)
q5 = 17 3.1416 42 2.7183 17 3.1416 42 2.7183 17 3.1416 42 2.7183 17 3.1416 42 2.7183 17 3.1416 42 2.7183
One thing I notice with the repmat solution is that I need to create the vector mean(A) for the function. I need to do the same thing without repmat and I want to be able to set up one function call for performing the calculation so I can use timeit. Since I can't index into the results of a function without assigning the output to a variable, I create an intermediate function meanones to help.
function y = meanones(A) mn = mean(A); y = A - mn(ones(1,size(A,1)),:);
Now I'm ready to do the timing.
findex = @() meanones(A); timeit(findex)
ans = 0.31389
Next see the timing calculation done using bsxfun.
fbsxfun = @() bsxfun(@minus,A,mean(A)); timeit(fbsxfun)
ans = 0.20569
In this example, bsxfun performs fastest. Now that you see bsxfun in action, can you think of uses for this function in your work? Let me know here.
Get the MATLAB code
Published with MATLAB® 7.6
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.