I want to add a column vector, length 66, to all columns in a matrix with about a million columns.
You would think that a single call to bsxfun would be the fastest way to do this - I certainly thought so - but batch processing a few thousand columns at a time seems to go somewhere between 2 or 3 times as fast...results are inconsistent (due to the details of memory management I suppose). I'm running in Matlab 2012a on a Mac.
a = rand(66,1E6); b = rand(66,1);
tic a = bsxfun(@plus,a,b); toc;
tic for ii=1:1E4:1E6 a(:,ii:ii+1E4-1) = bsxfun(@plus,a(:,ii:ii+1E4-1),b); end toc;
Is this behavior reproducible, and if so why does bsxfun self-optimise? Also, is there a nice way to choose the batch size, or do I just have to try it with different values on every computer I might want to run the code on.