Avoiding many if-Statetments in a for-loop

조회 수: 3 (최근 30일)
Kilian Helfenbein
Kilian Helfenbein 2019년 12월 11일
편집: Adam Danz 2019년 12월 11일
Hello everyone :)
I would like to run a for-loop over a vector. What to do with each element in the vector depends on many if-else statements. I simplified it like this:
clear variables;
rng default;
V1 = rand(365,1);
V2 = rand(365,1);
V2 = V2 <= 0.5;
V3 = rand(365,1);
V3 = V3 <= 0.3;
Vresult = zeros(length(V1),1);
tstart = 1;
tend = length(V1);
for t = tstart:tend
if V2(t) == 1
if V3(t) == 1
Vresult(t) = do_stuff(V1(t));
else
Vresult(t) = do_different_stuff(V1(t));
end
else
if V3(t) == 1
Vresult(t) = do_even_different_stuff(V1(t));
else
Vresult(t) = do_even_more_different_stuff(V1(t))
end
end
end
function [var_out] = do_stuff(var_in)
var_out = var_in * 5;
end
function [var_out] = do_different_stuff(var_in)
var_out = var_in + 3;
end
function [var_out] = do_even_different_stuff(var_in)
var_out = var_in / 4;
end
function [var_out] = do_even_more_different_stuff(var_in)
var_out = var_in - 2;
end
With even more if-else conditions and more calculations per statement it gets really confusing and unreadable. And I feel like I'm writing the same code over and over again as only some parts of the code are different in each statement and other parts stay the same.
How can I make my code more efficient, easier to read and maintainable? Sorry if this should be common knowledge :D
  댓글 수: 3
Kilian Helfenbein
Kilian Helfenbein 2019년 12월 11일
편집: Kilian Helfenbein 2019년 12월 11일
Hey Adam,
no, these are just for demo purpose.
In my actual Code I try to simulate a battery system with a PV system, which is integrated into virtual power plant. So there are a lot of variables to watch each step (like state of charge, PV generation, power consumption of the household, grid frequency and so on...).
But the calculations itselfs are pretty simple mostly. With min, max and the usual operators.
Adam Danz
Adam Danz 2019년 12월 11일
If those functions can be vectorized, the best approach is illustrated by the answer provided by "Adam". If you need to perform those functions on each value of V1 within a loop, the answer provided by myself ("Adam Danz") is a bit cleaner than nested conditional statements.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 12월 11일
I'm assuming the local functions provided are just demo functions. If those functions can be vectorized, it's very likely that you don't need a loop or any conditional statements at all.
Here's a method of performing a function on each element of vector V1 based on the values of V2(n) and V3(n) which are both logical vectors. It uses a switch-case instead of a series of conditional statements. The switch expression must be a scalar or char vector which is why I'm converting the logical values to char where 1 is true and 0 is false.
VAll = [V2,V3];
for t = tstart:tend
switch sprintf('%d %d',VAll(t,:))
case '1 1'
Vresult(t) = do_stuff(V1(t));
case '1 0'
Vresult(t) = do_different_stuff(V1(t));
case '0 1'
Vresult(t) = do_even_different_stuff(V1(t));
case '0 0'
Vresult(t) = do_even_more_different_stuff(V1(t));
otherwise
% This should never happen unless VAll is unexpected
error('Something is unexpected about the internal variable ''VAll''.')
end
end
  댓글 수: 2
Steven Lord
Steven Lord 2019년 12월 11일
Rather than turning your cases into char vectors I'd probably turn them into numbers. If V2 and V3 can only take on values 0 and 1, treat them like bits in a binary number.
% You may want to assert here to guard against V2 and V3
% taking values other than 0 or 1
VAll = [V2, V3];
v = polyval(VAll, 2);
switch v
case 0 % V2 = 0, V3 = 0
case 1 % V2 = 0, V3 = 1
case 2 % V2 = 1, V3 = 0
otherwise % V2 = 1, V3 = 1
end
Otherwise treat them like digits in an N-ary number.
Adam Danz
Adam Danz 2019년 12월 11일
편집: Adam Danz 2019년 12월 11일
I like the idea of using numbers rather than char vectors (clever use of polyval!). VAll is an nx2 logical matrix so the switch would need to be
VAll = [V2, V3];
for t = tstart:tend
switch polyval(VAll(t,:),2)
case 0 % V2 = 0, V3 = 0
case 1 % V2 = 0, V3 = 1
case 2 % V2 = 1, V3 = 0
otherwise % V2 = 1, V3 = 1
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2019년 12월 11일
편집: Adam 2019년 12월 11일
Something like this would seem to do the job, off the top of my head, though I may be over-simplifying it...
case1 = V2 && V3;
case2 = V2 && ~V3;
case3 = ~V2 && V3;
case4 = ~V2 && ~V3;
Vresult( case1 ) = do_stuff( V1( case1 ) );
Vresult( case2 ) = do_different_stuff( V1( case2 ) );
Vresult( case3 ) = do_even_different_stuff( V1( case3 ) );
Vresult( case4 ) = do_even_more_different_stuff( V1( case4 ) );

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by