How can I use switch case function to get decimal numbers?

4 次查看(过去 30 天)
M% Temperatur_C = [13,6169 14,5432 15,2356 17,2456 18,1887 19.9874 21,2765]
% Coeff -30C -20 -10 0 10 20 30 40 50
% nT 1.8 1.8 1.4 1.1 1.1 1 0.9 0.9 0.8
for A=1:(a-1)
switch (Temperatur_C(1,A))
case num2cell(-10.0:0)
disp('nT=1.4')
nT_NO(1,A) = 1.4;
case num2cell(0.1:4.99)
disp('nT=1.1')
nT_NO(1,A) = 1.1;
end
If I use round command I get the answer but that's not right for me because I want to be able to get the specific temperature not rounded.For example if the temp is 18,1887 it gets it as 20.
for A=1:(a-1)
switch round(Temperatur_C(1,A))
case num2cell(-10.0:0)
disp('nT=1.4')
nT_NO(1,A) = 1.4;
case num2cell(0.1:4.99)
disp('nT=1.1')
nT_NO(1,A) = 1.1;
end

采纳的回答

Guillaume
Guillaume 2019-11-7
I'm curious if you understand what that num2cell(-10.0:0) actually do in the case statement? If you don't and it's something you've been given by someone else don't use it.
It's not clear exactly what you're trying to do. I suspect it is this (minus the disps which are a bit pointless):
Breakpoints = [-Inf -30 -10 0 20 30 50 +Inf];
nT_Values = [NaN 1.8 1.4 1.1 1 0.9 0.8];
whichcolumn = discretize(Temperatur_C, Breakpoints);
nT_NO = nT_Values(whichcolumn);
%and if you want the equivalent to disp:
%celldisp(compose('nT=%g', nT_NO));
You can't do what you were trying to do with switch statements, you could do it with if...elseif
for idx = 1:numel(Temperature_C)
if Temperatur_C(idx) >= -30 & Temperatur_C(idx) < -10
nT_NO(idx) = 1.4;
elseif Temperatur_C(idx) >= -10 & Temperatur_C(idx) < 0
nT_NO(idx) = 1.1;
elseif ...etc
end
end
As you can see, using discretize is much simpler.
  4 个评论
Guillaume
Guillaume 2019-11-8
编辑:Guillaume 2019-11-8
Yes, as I've explained use discretize it's so much simpler that your two loops with endless elseif, plus the discretize call, as I wrote it, has the advantage that you get defined behaviour even if the temperature is outside your defined [-15, 45] range. With your code, nothing is ever assigned to the output variables (which may cause error later on as the variable may not even exist).
Your code can be simplified to:
breakpoints = [-Inf, -15, -5, 5, 15, 25, 35, 45, +Inf]; %temperature break points
nT_OX_Coeff = [ NaN, 1, 1.3, 1.5, 1.7, 2, 2.5, NaN];
nT_CO_Coeff = [ NaN, 1.4, 1.1, 1.1, 1.1, 0.9, 0.9, NaN];
nT_OX = discretize(Temperatur_C, breakpoints, nT_OX_Coeff);
nT_NO = discretize(Temperatur_C, breakpoints, nT_CO_Coeff);
%all done. No loop, no endless if...else...elseif
edit: lots of typos!
Steven Lord
Steven Lord 2019-11-8
In addition to the advantage Guillaume cited about what happens outside the bounds, this way if you need to add additional breakpoints (and the corresponding nT coefficients) you only need to modify the lines where the data was created. You don't need to modify the code at all (and run the risk of updating the large if / else / elseif / end statement for one gas but not the other.)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by