How to simplify too much of if

1 Ansicht (letzte 30 Tage)
Danial
Danial am 29 Jan. 2023
Bearbeitet: Matt J am 29 Jan. 2023
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

Antworten (3)

Walter Roberson
Walter Roberson am 29 Jan. 2023
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 am 29 Jan. 2023
Bearbeitet: Jan am 29 Jan. 2023
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 am 29 Jan. 2023
Bearbeitet: Matt J am 29 Jan. 2023
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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by