You can define your own class that allows bsxfun to be used automatically:
classdef bdouble < double %BDOUBLE double with automatic broadcasting enabled methods function obj=bdouble(data) obj = obj@double (data); end function r=plus(x,y) r=bsxfun(@plus,x,y); end function r=minus(x,y) r=bsxfun(@minus,x,y); end function r=power(x,y) r=bsxfun(@power,x,y); end function r=times(x,y) r=bsxfun(@times,x,y); end function r=rdivide(x,y) r=rdivide(@rdivide,x,y); end function r=ldivide(x,y) r=ldivide(@ldivide,x,y); end end end
Using:
testFunction(bdouble([1:3]),bdouble([1:4]'),bdouble(cat(3,1,1)),bdouble(1))
If you prefer the bsxfun syntax, you can put this on top:
function varargout=nbsxfun(fun,varargin) varargout=cell(1:nargout); for idx=1:numel(varargin) if isa(varargin{idx},'double') varargin{idx}=bdouble(varargin{idx}); end end varargout{1:max(nargout,1)}=fun(varargin{:}); end
Example:
n=nbsxfun(testFunction,[1:3],[1:4]',cat(3,1,1),4)
Daniel
source share