필터 지우기
필터 지우기

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

조회 수: 9 (최근 30일)
Kofial
Kofial 2019년 11월 7일
댓글: Steven Lord 2019년 11월 8일
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