# Webinar 2015 Profiling function vectorization in Matlab/Octave

Matlab is a productive tool for interacting with and performing calculations on multi-dimensional numeric arrays. When performing calculations on multi-dimensional arrays some forms of the procedures do not scale well and result in a poor performance for large arrays. In this seminar we will explore profiling tools for monitoring the performance of operations in Matlab and compare different forms of simple operations on three dimensional numeric arrays. The example data are taken from an electroencephalograophic (EEG) recording and the operations performed are relevant to typical procedures found in EEG processing pipelines. Although the specific examples come from EEG research, the procedures explored here are widely generalizable.

Example: given a three dimensional “data” array, subtracting the row means from the original array.

The temptation is to try: outdata=data-mean(data,2);

…generating the error “Matrix dimensions must agree.”

The explicit nested for loop form is: for rind=1:size(data,1); for pind=1:size(data,3); outdata(rind,:,pind)=data(rind,:,pind)-mean(data(rind,:,pind),2); end; end;

Using repmat expansion of an intermediate array: outdata=data-repmat(mean(data,2),1,size(data,2));

Using bsxfun singlton dimension expansion: outdata=bsxfun(@minus,data,mean(data,2));

From profiling these procedures the benefits of using bsxfun become clear in terms of performance. Specifically, a single call to minus without the need to generate an intermediate array. During this seminar the example of using bsxfun for the mean subtraction will then be expanded to a more complex procedure of correlating rows of the data array. Other topics covered in this seminar will include options for parallel processing and performance comparisons using Octave on SHARCNET.