How to simplify too much of if

1 回表示 (過去 30 日間)
Danial
Danial 2023 年 1 月 29 日
編集済み: Matt J 2023 年 1 月 29 日
I assume that my code below could be simplified to something more shorter but i cant think of how.
if (0<S)&&(S<=10)
if AggS==10
if fap==40
if fwcr==0.5
fp=39;
else
fp=45;
end
elseif fap==60
if fwcr==0.5
fp=33;
else
fp=36 ;
end
elseif fap==80
if fwcr==0.5
fp=27;
else
fp=30;
end
end
else
if fap==40
if fwcr==0.5
fp=29;
else
fp=33;
end
elseif fap==60
if fwcr==0.5
fp=24;
else
fp=28;
end
elseif fap==80
if fwcr==0.5
fp=20;
else
fp=24;
end
end
end
elseif (10<S)&&(S<=30)
if AggS==10
if fap==40
if fwcr==0.5
fp=41;
else
fp=46;
end
elseif fap==60
if fwcr==0.5
fp=34;
else
fp=38;
end
elseif fap==80
if fwcr==0.5
fp=29;
else
fp=32;
end
end
else
if fap==40
if fwcr==0.5
fp=32;
else
fp=36;
end
elseif fap==60
if fwcr==0.5
fp=25;
else
fp=30;
end
elseif fap==80
if fwcr==0.5
fp=22;
else
fp=25;
end
end
end
elseif (30<S)&&(S<=60)
if AggS==10
if fap==40
if fwcr==0.5
fp=36;
else
fp=49;
end
elseif fap==60
if fwcr==0.5
fp=38;
else
fp=41;
end
elseif fap==80
if fwcr==0.5
fp=31;
else
fp=34;
end
end
else
if fap==40
if fwcr==0.5
fp=35;
else
fp=39;
end
elseif fap==60
if fwcr==0.5
fp=29;
else
fp=32;
end
elseif fap==80
if fwcr==0.5
fp=24;
else
fp=37;
end
end
end
elseif (60<S)&&(S<=180)
if AggS==10
if fap==40
if fwcr==0.5
fp=50;
else
fp=55;
end
elseif fap==60
if fwcr==0.5
fp=42;
else
fp=45;
end
elseif fap==80
if fwcr==0.5
fp=35;
else
fp=37;
end
end
else
if fap==40
if fwcr==0.5
fp=41;
else
fp=44;
end
elseif fap==60
if fwcr==0.5
fp=32;
else
fp=36;
end
elseif fap==80
if fwcr==0.5
fp=27;
else
fp=31;
end
end
end
end
  1 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 1 月 29 日
Maybe you can try
switch .. case

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2023 年 1 月 29 日
Use look-up tables. For example the final elseif can be encoded as
{[40 50 55
60 42 45
80 35 37]
[40 41 44
60 32 36
80 27 31]}
with the logic that you index the cell by {2 - (AggS==10)} and then you ismember(fap, CellContents(:,1)) to figure out which row you are talking about, and you pull out column 2 if fwcr==0.5 and column 3 otherwise.
You can use discretize to figure out which S case you are working with so pretty much everything can go into the same data structure.

Jan
Jan 2023 年 1 月 29 日
編集済み: Jan 2023 年 1 月 29 日
Change:
if (0<S)&&(S<=10)
if AggS==10
if fap==40
if fwcr==0.5
fp=39;
else
fp=45;
end
elseif fap==60
if fwcr==0.5
fp=33;
else
fp=36 ;
end
elseif fap==80
if fwcr==0.5
fp=27;
else
fp=30;
end
end
else
if fap==40
if fwcr==0.5
fp=29;
else
fp=33;
end
elseif fap==60
if fwcr==0.5
fp=24;
else
fp=28;
end
elseif fap==80
if fwcr==0.5
fp=20;
else
fp=24;
end
end
end
...
to:
if (0<S) && (S<=10)
if AggS == 10
switch fap
case 40
v = [45, 39];
case 60
v = [36, 33];
case 80
v = [30, 27];
end
else
switch fap
case 40
v = [33, 29];
case 60
v = [28, 24];
case 80
v = [24, 20];
end
end
...
end
fp = v((fwcr == 0.5) + 1);
A next step:
if (0<S) && (S<=10)
if AggS == 10
v = [45, 39; 36, 33; 30, 27];
else
v = [33, 29; 28, 24; 24, 20];
end
...
end
i1 = (fap == [40, 60, 80]);
i2 = (fwcr == 0.5) + 1;
fp = v(i1, i2);
Indexing in arrays is more compact than a pile of if conditions.

Matt J
Matt J 2023 年 1 月 29 日
編集済み: Matt J 2023 年 1 月 29 日
FP=[39 45 33 36 27 30, 29,33,24,28,20,24,...
41 46 34,38,29,32,32,36,25,30,22,25,36,49,38,41,31,34,35,39,....
29,32,24,37,50,55,42,45,35,37,41,44,32,36,27,31];
FP=reshape(FP,2,3,2,4);
G=griddedInterpolant({1:2, [40,60,80],1:2,1:4},FP);
fap=40;AggS=10;fwcr=0.5; S=30; %hypothetical input
fp=G(2-(fwcr==0.5),fap,2-(AggS==10),...
discretize(S,[0,10,30,60,180],'IncludedEdge', 'right'))
fp = 41

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by