computational neuroscience, vision research, matlab, and whatnot

Obscure Matlab function #1: bsxfun

Here’s a first in what might become a regular feature on xcorr: an exposition of some of Matlab’s obscure but quite useful functions. Today’s function: bsxfun. The Matlab documentation states that bsxfun’s function is to:

Apply element-by-element binary operation to two arrays with singleton expansion enabled

This is not a very helpful definition. What bsxfun(@op,A,B) does is virtually expand A and B to a common size op(Abig,Bbig). It obviates the need for repmat and outer products in most scenarios. For example, to center the columns of X, you might use:

X = X - repmat(mean(X),size(X,1),1);

Or:

X = X - ones(size(X,1),1)*mean(X);

Internally this constructs the intermediate matrix on the right hand side, which is wasteful. bsxfun can be used to center the columns of a matrix without intermediate matrices:

X = bsxfun(@minus,X,mean(X));

How bsxfun virtually replicates matrices is straightforward. In the previous example, the A matrix as size MxN, while the B matrix has size 1xN. bsxfun virtually expands matrix B along its dimensions of size 1 (the singleton dimensions). to match the dimensions of A.

Another example: construct the matrix of size MxM: [1,2,3,...;0,1,2,...;-1,0,1,...]:

themat = bsxfun(@minus,(1:M),(0:M-1)');

Comments on: "Obscure Matlab function #1: bsxfun" (1)

  1. [...] You can read about bsxfun here if you are unfamiliar with this function. Because the results of whitening can be noisy, a fudge factor is used so that eigenvectors associated with small eigenvalues do not get overamplified. Thus the whitening is only approximate. Here’s an image patch whitened this way: [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s