How to overload all functions and operators of a new class?

조회 수: 7 (최근 30일)
Jin Sun
Jin Sun 2016년 3월 24일
댓글: Jin Sun 2016년 3월 26일
Hi, All,
I would like to design a class of multi-dimensional arrays that automatically does a number of things. In particular, automatic array extension as the "bsxfun" function does, only without having to call "bsxfun" explicitly. For example: A=myclass(rand(4,4)); b=myclass(rand(4,1)); C=A.*b; where C will be an 4x4 array given by multiplying elementwise each column of A by b, just like C=bsxfun(@times,A,b) will produce for generic arrays A and b.
Moreover, the code L=log(A); should produce logarithm of each element of A, where L is of course an array of myclass. I hope ALL compatible functions and operators should simultaneously be overloaded. How can I do that?
This class potentially could SIGNIFICANTLY simplify MOST of my past MATLAB coding experience. However, So far I have not been able to do that. I have to overload every specific functions one at a time, which is too tedious and error-prone.
Thank you for any answers.
Jin

채택된 답변

LY Cao
LY Cao 2016년 3월 26일
for r2015b or r2016a, you can do this:
builtin('_useSingletonExpansion',1) % undocumented
a = rand(4); b = rand(4,1);
c = a.*b; % bsxfun(@times,a,b)
d = a + b; % bsxfun(@plus,a,b)
e = a./b; % bxfun(@rdivide,a,b)
f = a == b; % bsxfun(@eq,a,b)
g = atan2(a,b); % bsxfun(@atan2,a,b)
h = a.^b; % bsxfun(@power,a,b)
i = mod(a,b); % bsxfun(@mod,a,b)
k = a & b; % bsxfun(@and,a,b)
......
  댓글 수: 1
Jin Sun
Jin Sun 2016년 3월 26일
This is good news. Will try it out on the latest version. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by