Project

General

Profile

10 anderm8
function bsxfunEx
%% This example shows the use of bsxfun

% C = bsxfun(fun,A,B) applies an element-by-element binary operation to
% arrays A and B, with singleton expansion enabled. fun is a function
% handle, and can either be a MATLAB file function or one of a selected number
% of built in functions

% Copyright 2015 The MathWorks, Inc.

A = magic(4000);
t1 = 0;
t2 = 0;

for ii = 1:10
tic
Anew1 = A - repmat(mean(A),length(A),1);
t1 = t1+toc;

tic
Anew2 = bsxfun(@minus, A, mean(A));
t2 = t2+toc;
end
fprintf(1,'t1: %fs\nt2: %fs\n',t1,t2);

% Verify that they're equal
assert(isequal(Anew1,Anew2),'Anew1 is not equal to Anew2')
% Copyright 2015 The MathWorks, Inc.