Can we define operators in MATLAB?
Show older comments
I started learning OOP in MATLAB and I saw that we can either use the colon(A,B) syntax or simply A:B. This kind of method works for other operators and we may even redefine how they work.
My question is: can we somehow achieve that MATLAB recognises some other symbols we define? For example, I want to use ° for dyadic product in my class and if I typed A°B, dyadic(A,B) would be executed on objects A and B.
Accepted Answer
More Answers (2)
Matt J
on 19 Dec 2014
There are fake things you can do. I don't really recommend them, but just for fun, I create a function below that makes the '$' symbol act like the '+' symbol,
>> A=1;B=2;
>> Q C=A$B
>> ans=C
ans =
3
function Q(varargin)
cmd=[varargin{:} ';'];
cmd=strrep(cmd,'$','+');
evalin('caller',cmd)
2 Comments
Sean de Wolski
on 19 Dec 2014
Ughh that hurts...
Matt J
on 20 Dec 2014
Yeah, just an illustration of some of the horrible things you can do.
Sean de Wolski
on 19 Dec 2014
Edited: Sean de Wolski
on 19 Dec 2014
No, you can only overload the current operators. I think this is pretty close to the full list
le,lt,gt,ge,eq,ne,colon,end,times,mtimes,ldivide
rdivide,mrdivide,mldivide,power,mpower,subsref,subsindex,subsasgn
Categories
Find more on Number Theory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!