From: Matt J on
Similar to Bruno's bsxops, you could also use the function below, which makes use my MatrixObj class

http://www.mathworks.com/matlabcentral/fileexchange/26611-on-the-fly-definition-of-custom-matrix-objects

An example of use is given in the help text.



function v=bsxobj(x)
%BSXOBJ - Creates a data type for which matrix operations like addition,
%subtraction, etc... behave as in BSXFUN()
%
% v=bsxobj(x)
%
%Converts array x to a @MatrixObj data type v capable of bsxfun-like math
%operations.
%
%EXAMPLE:
%
% >> v=bsxobj([1;2;3i]);
%
% >> v+v'
%
% ans =
%
% 2.0000 3.0000 1.0000 - 3.0000i
% 3.0000 4.0000 2.0000 - 3.0000i
% 1.0000 + 3.0000i 2.0000 + 3.0000i 0



v=MatrixObj;

v.Params=x;
v.Ops.minus=@(A,B) bsxfun(@minus,Arg(A),Arg(B));
v.Ops.plus= @(A,B) bsxfun(@plus,Arg(A),Arg(B));
v.Ops.times=@(A,B) bsxfun(@times,Arg(A),Arg(B));
v.Ops.rdivide= @(A,B) bsxfun(@rdivide,Arg(A),Arg(B));
v.Ops.ldivide= @(A,B) bsxfun(@ldivide,Arg(A),Arg(B));

v.Ops.power=@(A,B) bsxfun(@power,Arg(A),Arg(B));



v.Ops.eq=@(A,B) bsxfun(@eq,Arg(A),Arg(B));
v.Ops.ne= @(A,B) bsxfun(@ne,Arg(A),Arg(B));
v.Ops.lt=@(A,B) bsxfun(@lt,Arg(A),Arg(B));
v.Ops.le= @(A,B) bsxfun(@le,Arg(A),Arg(B));
v.Ops.gt= @(A,B) bsxfun(@gt,Arg(A),Arg(B));
v.Ops.ge=@(A,B) bsxfun(@ge,Arg(A),Arg(B));
v.Ops.and= @(A,B) bsxfun(@and,Arg(A),Arg(B));
v.Ops.or=@(A,B) bsxfun(@or,Arg(A),Arg(B));


v.Ops.transpose= @bsxobj_tranpose;
v.Ops.ctranspose= @bsxobj_ctranpose;
v.Ops.not= @bsxobj_not;
v.Ops.uminus= @bsxobj_uminus;

v.Ops.sum= @bsxobj_sum;


v.Ops.size=@(obj) size(obj.Params);




function obj=bsxobj_tranpose(obj)

obj.Params=obj.Params.';



function obj=bsxobj_ctranpose(obj)

obj.Params=obj.Params';


function obj=bsxobj_uminus(obj)

obj.Params=-obj.Params;



function obj=bsxobj_not(obj)

obj.Params=~obj.Params;


function out=bsxobj_sum(obj,varargin)

out=sum(obj.Params,varargin{:});




function out=Arg(X)

out=X;
if isa(X,'MatrixObj')
out=X.Params;
end