Non-scalar enumerations of class double
6 次查看(过去 30 天)
显示 更早的评论
Why am I not able to create an enumeration of double arrays? Consider the following simple case.
classdef Quaternion < double
enumeration
U([ 1, 0; 0, 1])
I([1i, 0; 0,-1i])
J([ 0,-1; 1, 0])
K([ 0,1i;1i, 0])
end
end
To me, everything seems in order. The enumerated values are distinct, and of the appropriate class. But when I call the constructor I get the following error.
>> u = Quaternion.
In class 'Quaternion', enumeration member named 'U' must be scalar.
I scoured the documentation, and couldn't find anything that explicitly stated this was not possible.
If this is simply not possible, perhaps Mathworks should consider making this a new feature. Seems like a no-brainer to me.
0 个评论
回答(1 个)
Captain Karnage
2023-8-7
There's a few problems with attempting to do this in MATLAB. First, though you can make an enumeration a subclass of a built-in type - you can't do it unless your enumerations are each a specific scalar value of the superclass type. Instead, you should make it not a subclass of a built in type and instead make properties of that type, as follows:
classdef Quaternion
enumeration
U( [ 1, 0; 0, 1] )
I( [ 1i, 0; 0, -1i] )
J( [ 0, -1; 1, 0] )
K( [ 0, 1i; 1i, 0] )
end
properties
q double;
end
methods
function obj = Quaternion(q)
arguments
q (2, 2) double;
end
obj.q = q;
end
end
end
You could also make the constructor have 2 or 4 inputs, each representing either a row or individual element of your matrix, if you wanted - and could map that to a single or multiple properties as well. For example, the constructor could be obj = Quaternion(a,b,c,d) and then set obj.q = [ a, b; c, d] and validate that each input is a 1x1 scalar if you want to do validation.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Enumerations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!