% bsxfun20062006 does binary operation with singleton expansion (uses bsxarg)
%--------------------------------------------------------------------------
% MATLAB (R) is a trademark of The Mathworks (R) Corporation
%
% Function: bsxfun20062006
% Filename: bsxfun20062006.m
% Programmer: James Tursa, modified by Bruno Luong
% Version: 1.0
% Date: February 09, 2008
% Copyright: (c) 2008 by James Tursa, All Rights Reserved
% Permission: Permission is granted to freely distribute and use this code
% as long as the header information is included.
%
% A poor-man's replacement for bsxfun2006 for those users with earlier versions
% of MATLAB that do not have this function available to them. Uses the mex
% routine bsxarg to physically generate the singleton expanded arrays, and
% then calls the function. This method is not as fast as the MATLAB
% intrinsic bsxfun2006, but at least it gives you the same functionality.
%
% Apply element-by-element binary operation to two arrays with singleton
% expansion enabled.
%
% bsxfun2006 is an m-file function that mimics the MATLAB intrinsic bsxfun2006.
% The m-file bsxfun2006 differs from the MATLAB bsxfun2006 in the following ways:
% - Limited to 12 dimensions
% - Allows the 1st input to be a char string as well as a function handle
% - Physically does the singleton expansion of each input array
%
% This bsxfun2006 m-file is mainly intended for users with older versions of
% MATLAB that do not have the MATLAB intrinsic bsxfun2006 available.
%
% REQUIREMENT: You must build bsxarg first (see bsxarg.c file)
%
% The following are the relavent excerpts from the MATLAB doc:
%
% Syntax
%
% C = bsxfun2006(fun,A,B)
%
% Description
%
% C = bsxfun2006(fun,A,B) applies an element-by-element binary operation to
% arrays A and B, with singleton expansion enabled. fun is a function handle or,
% a character string giving the name of a function, and can either be an M-file
% function or one of the following built-in functions:
%
% @plus Plus
% @minus Minus
% @times Array multiply
% @rdivide Right array divide
% @ldivide Left array divide
% @power Array power
% @max Binary maximum
% @min Binary minimum
% @rem Remainder after division
% @mod Modulus after division
% @atan2 Four quadrant inverse tangent
% @hypot Square root of sum of squares
% @eq Equal
% @ne Not equal
% @lt Less than
% @le Less than or equal to
% @gt Greater than
% @ge Greater than or equal to
% @and Element-wise logical AND
% @or Element-wise logical OR
% @xor Logical exclusive OR
%
% If an M-file function is specified, it must be able to accept two input
% arrays of the same size as the result C.
%
% Each dimension of A and B must either be equal to each other, or equal to 1.
% Whenever a dimension of A or B is singleton (equal to 1), the array is
% replicated along the dimension to match the other array. The array
% may be diminished if the corresponding dimension of the other array is 0.
%
% The size of the output array C is equal to:
%
% max(size(A),size(B)).*(size(A)>0 & size(B)>0).
%
% Examples
%
% In this example, bsxfun2006 is used to subtract the column means from the matrix A.
%
% A = magic(5);
% A = bsxfun2006(@minus, A, mean(A))
% A =
% 4 11 -12 -5 2
% 10 -8 -6 1 3
% -9 -7 0 7 9
% -3 -1 6 8 -10
% -2 5 12 -11 -4
%
%--------------------------------------------------------------------------
function C = bsxfun2006(fun,A,B)
persistent bsxfunexist
% Compatible 2006B and anterior version, 32 bits only
% [AX BX] = bsxarg(A,B);
% C = fun(AX,BX);
% Native Matlab bsxfun
% C = bsxfun(fun,A,B);
if isempty(bsxfunexist)
bsxfunexist = false;
bsxfunloc = which('bsxfun');
if ~isempty(strfind(bsxfunloc,'built-in'))
bsxfunexist = true;
end
if ~bsxfunexist
warning('bsxfun2006:BuiltInNotFound', ...
'bsxfun2006: compatible replacement of bsxfun will be used');
end
end
if ~bsxfunexist % 2006B and prior
if( nargin ~= 3 )
error('Need 3 arguments for bsxfun2006')
end
[AX BX] = bsxarg(A,B);
if( ischar(fun) )
C = eval([fun '(AX,BX)']);
else
C = fun(AX,BX);
end
return
else
C = bsxfun(fun,A,B); % Call builtin bsxfun, featuring from 2007A
end
end