필터 지우기
필터 지우기

Non-scalar enumerations of class double

조회 수: 2 (최근 30일)
Daniel
Daniel 2013년 6월 19일
답변: Captain Karnage 2023년 8월 7일
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.

답변 (1개)

Captain Karnage
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.

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by