Matlab 让一个开关盒调用开关盒,而不是运行所有开关盒
下面你可以看到我的代码。当我运行这个时,我从所有三个switch语句中得到答案。我只想从一个switch语句中得到一个答案,而忽略另外两个。我怎样才能做到这一点Matlab 让一个开关盒调用开关盒,而不是运行所有开关盒,matlab,switch-statement,Matlab,Switch Statement,下面你可以看到我的代码。当我运行这个时,我从所有三个switch语句中得到答案。我只想从一个switch语句中得到一个答案,而忽略另外两个。我怎样才能做到这一点 function grashofrough(a, b, c, d) % The variables a,b,c, and d are variables to % represent the length of each length Lengths = [ a b c d ]; S=min(Lengths); L=max(L
function grashofrough(a, b, c, d)
% The variables a,b,c, and d are variables to
% represent the length of each length
Lengths = [ a b c d ];
S=min(Lengths);
L=max(Lengths);
%This will direct to 'Grashof' cases
Grashof = L+S < sum(Lengths)-(S+L);
%This will direct to 'Non-grashof' cases
NGRASH = L+S > sum(Lengths)-(S+L);
%This will direct to 'Special Grashof' cases
SpecGrashof = L+S == sum(Lengths)-(S+L);
switch Grashof
case S == a
disp("GCCC")
case S == b
disp("GCRR")
case S == c
disp("GRCR")
case S == d
disp("GRRC")
otherwise
return
end
switch NGRASH
case L == a
disp("RRR1")
case L == b
disp("RRR1")
case L == c
disp("RRR3")
case L == d
disp("RRR4")
otherwise
return
end
switch SpecGrashof
case S == a
disp("SCCC")
case S == b
disp("SCRR")
case S == c
disp("SRCR")
case S == d
disp("SRRC")
otherwise
return
end
函数grashofrough(a,b,c,d)
%变量a、b、c和d是
%表示每个长度的长度
长度=[a b c d];
S=最小值(长度);
L=最大(长度);
%这将指向“Grashof”案例
Grashof=L+S<和(长度)-(S+L);
%这将直接导致“非格拉肖夫”案件
NGRASH=L+S>总和(长度)-(S+L);
%这将直接针对“特殊格拉肖夫”案例
SpecGrashof=L+S==和(长度)-(S+L);
开关格拉肖夫
案例S==a
disp(“GCCC”)
案例S==b
disp(“GCRR”)
案例S==c
disp(“GRCR”)
案例S==d
disp(“GRRC”)
否则
返回
结束
开关皮疹
案例L==a
disp(“RRR1”)
案例L==b
disp(“RRR1”)
案例L==c
disp(“RRR3”)
案例L==d
disp(“RRR4”)
否则
返回
结束
开关规格
案例S==a
disp(“SCCC”)
案例S==b
disp(“SCRR”)
案例S==c
disp(“SRCR”)
案例S==d
disp(“SRRC”)
否则
返回
结束
我想您的意思是根据
Grashof
,NGRASH
或SpecGrashof
中的哪一个是正确的来选择一个开关情况。对此,需要使用if
语句
您使用的开关
不正确,开关
的参数是一个变量,各种情况都是该变量的可能值。我建议你
这是你打算写的:
if Grashof
switch S
case a
disp("GCCC")
case b
disp("GCRR")
case c
disp("GRCR")
case d
disp("GRRC")
otherwise
return
end
elseif NGRASH
switch L
case a
disp("RRR1")
case b
disp("RRR1")
case c
disp("RRR3")
case d
disp("RRR4")
otherwise
return
end
else % SpecGrashof must be true here, no need to test for it
switch S
case a
disp("SCCC")
case b
disp("SCRR")
case c
disp("SRCR")
case d
disp("SRRC")
otherwise
return
end
end
但考虑到你对这三种情况的定义:
Grashof = L+S < sum(Lengths)-(S+L); %This will direct to 'Grashof' cases
NGRASH = L+S > sum(Lengths)-(S+L); %This will direct to 'Non-grashof' cases
SpecGrashof = L+S == sum(Lengths)-(S+L); %This will direct to 'Special Grashof' cases
其中,在每个案例中,您将switch语句置于上述
S
或L
上。我认为您的意思是根据Grashof
中的哪一个选择一个切换案例,NGRASH
或SpecGrashof
为真。对此,需要使用if
语句
您使用的开关
不正确,开关
的参数是一个变量,各种情况都是该变量的可能值。我建议你
这是你打算写的:
if Grashof
switch S
case a
disp("GCCC")
case b
disp("GCRR")
case c
disp("GRCR")
case d
disp("GRRC")
otherwise
return
end
elseif NGRASH
switch L
case a
disp("RRR1")
case b
disp("RRR1")
case c
disp("RRR3")
case d
disp("RRR4")
otherwise
return
end
else % SpecGrashof must be true here, no need to test for it
switch S
case a
disp("SCCC")
case b
disp("SCRR")
case c
disp("SRCR")
case d
disp("SRRC")
otherwise
return
end
end
但考虑到你对这三种情况的定义:
Grashof = L+S < sum(Lengths)-(S+L); %This will direct to 'Grashof' cases
NGRASH = L+S > sum(Lengths)-(S+L); %This will direct to 'Non-grashof' cases
SpecGrashof = L+S == sum(Lengths)-(S+L); %This will direct to 'Special Grashof' cases
在每种情况下,您都将switch语句置于上面的
S
或L
上。我没有看到您的解决方案!我会删除我的。我喜欢符号(K)方法。我没有看到你的解决方案!我会删除我的。我喜欢符号(K)方法。