Is there any means to overload '* (multiplication by the transpose matrix) with my own matrix class?

2 views (last 30 days)
Hello I'm trying to make my own matrix class in Matlab. I succeed to overload the operator transpose' and mtimes, so let's A an instance of my matrix class and x, an instance of the matlab matrix. y=A'*x is really slow compared to y=A*x; Is there any means to overload the operator '*, (i.e multiplication by the transpose of an instance of my matrix class)

Accepted Answer

James Tursa
James Tursa on 19 Jul 2016
You could create a special method for this. E.g., tmtimes. Then instead of doing y = A'*x you would do tmtimes(A,x).
Or you could add a property to your class that keeps track of transpose status. Then when A' happens you simply flip the transpose property of your matrix object instead of physically transposing it. Then when the *x part happens you can check the transpose property flag of the operands. Downside of this is you would now have to check this transpose property flag for every operation you do.
  1 Comment
Nicolas Bellot
Nicolas Bellot on 20 Jul 2016
Edited: Nicolas Bellot on 20 Jul 2016
Thanks for the answer, I already tried the first solution, creating a mtimes_trans(A,x,tranpose_flag) with the flag saying if we do A*x or A'*x.
But maybe your second proposition is the best, instead of passing the tranpose_flag, it should be an attribute of my class. So if I call transpose(A) with no output argument, it just put the transpose_flag to on but if we do C=tranpose(A), I create a new matrix which is the transpose of my matrix but with the transpose_flag to off. So when I overload mtimes at the end of my function, I put the transpose_flag to off. So, I can do in a row y1=A'*x1; y2=A*x2; my only drawback is if someone do A' with no return argument, it will transpose A, so A will become the transpose of A which is not the usual behaviour of the matlab transpose operator.
But maybe it's a good trade-off

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!