MATLAB: Correct use of arrayfun

arraycell arraysMATLAB

Suppose that I have a matrix a, and I want to do an operation on it, e.g. subtracting the mean of its columns from each element of the matrix. How can i do that by using the command arrayfun?
a = [ 3 6 ; 6 5 ]
arrayfun(@(x) a(i,j)-mean(a(i,:)),a,'UniformOutput',false)

Best Answer

Well first of all, arrayfun should be a last resort. There are much more optimal ways of doing the mean subtraction you describe as well as many other operations like it. In R2016b or higher,
a-mean(a,1),
else if older than R2016b,
bsxfun(@minus, a, mean(a,1))
But, if you really must use arrayfun, it can be done, for example, as follows
cell2mat( arrayfun(@(i) a(:,i)-mean(a(:,i)), 1:size(a,2), 'uni',0) )