Override subsasgn of MATLABs double?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Consider the following statement:
x = [1 2;3 4];
y = myclass([5 6]);
x(1:2) = y
MATLABs default behavior is:
- x(1:2) = ... returns a double
However what I want is:
- x(1:2) = ... returns a myclass
How can I exchange subsasgn in MATLABs default double class?
5 Kommentare
Antworten (2)
James Tursa
am 23 Apr. 2020
Overriding subasgn for a native class like double is a very bad idea, because it affects everything, not just the code that you wrote. If you want a myclass object, then use myclass( ) method explicitly to create it. That is how the language is set up to work.
4 Kommentare
James Tursa
am 23 Apr. 2020
Instead of working with code snippets, can you describe the behavior you are after at a higher level, maybe with some pseudo-code examples? Then maybe we can steer you in the right direction to acheive your overall objective.
Steven Lord
am 23 Apr. 2020
As James Tursa stated, overriding methods for the double class is a Bad Idea, and trying to override indexing for double is a Very Bad Idea.
If you want all the variables in your function to be of the same class as the input (double for a double precision input, myclass for a myclass input, etc.) I would instead initialize all the variables in your function based on the class of the input. One way to do this is to liberally sprinkle cast calls throughout your code. A different way, since I'm hoping you're already preallocating the variables in your code using functions like ones, zeros, eye, etc. is to specify the class input to those functions. MATLAB already knows how create variables of built-in types this way (and will do so without first creating a larger double array and casting it to the selected type.)
A = ones(4, 5, 'int8')
B = eye(6, 'uint64')
C = rand(3, 3, 'single')
x = int32(5);
D = zeros(2, 6, class(x)) % or
E = zeros(2, 6, 'like', x)
To enable this for your class, you need to define a few methods on your class. If you can't or don't want to define those methods, you could cast. All of the following examples showing this use the sym object from Symbolic Math Toolbox.
F = inf(5, 5, 'sym') % sym defines a static inf method
G = randi(5, [4 4], 'sym') % Fails because sym doesn't define a randi method
H = cast(randi(5, [4 4]), 'sym') % works
3 Kommentare
Steven Lord
am 23 Apr. 2020
Possible? Yes. Maybe not to do what you want your overload to do (I'd need to think a bit more about that) but possible.
But if you were to do that, you are invalidating an assumption about how MATLAB works, that the type of the left-hand side of an assignment statement doesn't change when you assign into a piece of it. Is that an important or often-used assumption? I don't know offhand. Would invalidating it lead to bugs in functions that were working perfectly fine before? Possibly.
When or if you update your code to preallocate variables to the class of the input argument, you won't need to adjust it again based on whether the input is a double, single, myclass, etc.
f = @(x) ones(2, 3, 'like', x);
outputDouble = f(5)
outputUint64 = f(uint64(5))
outputSym = f(sym(5))
whos output*
The body of f doesn't change but because it's called with different input arguments the class of the output is different.
And of course those construction methods aren't the only ones that you can overload. You can overload the operators for myclass to count how many times they're called.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!