how to generate a multiple if-else if statements for n times?
조회 수: 9 (최근 30일)
이전 댓글 표시
so basically i have 2 if-elseif statement in while loop, for each while loop there is rem function (in my line program is 'rem(calc,2)' ) for picking if-elseif statements sequentially (for ex: 1st loop> rem = 0 > take 2nd if > 2nd loop > rem = 1 > take 1st if > and so on). i will show the code about it below
what i need is i want to generate N-times of if-elseif statement while rem function is still follows ( in need rem(calc,N) ) in one while loop. the input is manually input by excel ( for general ideas, the attachment program are one of my program with 2 if-elseif statements in one while loop. It's about generating two ideal deflection given by 2 force loop system )
how do i generate it?
my program is standard build, should i change it into Object Oriented Programming? if so, how?
to be honest i'm still new in matlab, and recently knew about object oriented programming, any guidance will be much help, thanks :)
while true
calc = calc + 1
loop(calc)=loop(calc-1)+1
rem_f = rem(calc,2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% THIS PART IS CORE OF THE PROBLEM
if rem_f == 1
if last_tol1 < 0.90
act_f = act_f + afl_inc1
elseif last_tol1 > 1
act_f = act_f + afl_dec1
else
act_f = act_f
end
else
if last_tol2 < 0.90
act_f = act_f + afl_inc2
elseif last_tol2 > 1
act_f = act_f + afl_dec2
else
act_f = act_f
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
defl_loop_ans = kkk*act_f; %deflection toward acting dofs
%%% Mapping generated Deflection
% #1 Deflection & Acting Force data each dof
defl_loop = zeros(big_dof,1); %empty matrix for after added force loop
for_loop = zeros(big_dof,1);
for mm=1:toa_dof;
dof_act=ta_dof(mm,1);
def_val=defl_loop_ans(mm,1);
for_val=act_f(mm,1);
defl_loop(dof_act,1)=defl_loop(dof_act,1)+def_val;
for_loop(dof_act,1)=for_loop(dof_act,1)+for_val;
end
% #2 Deflection for each axis
defl = defl_loop(:,1);
defl_x = defl(1:3:big_dof);
defl_y = defl(2:3:big_dof);
defl_t = defl(3:3:big_dof);
% #3 Post-Deflect position
newp_x = pos_x+defl_x;
newp_y = pos_y+defl_y;
newp_t = (pos_t + defl_t); %1 RAD = 57.296 DEG
%deflection position
adefl_lo= zeros(big_nod,3);
adefl_lo(:,1) = defl_x;
adefl_lo(:,2) = defl_y;
adefl_lo(:,3) = defl_t
delo_1 = adefl_lo((ceil(data2(:,7)/3)),data2(:,6));
delo_2 = adefl_lo((ceil(data2(:,17)/3)),data2(:,16));
tol_1(calc)=((delo_1)/(idp_1 - dpint_1))
tol_2(calc)=((delo_2)/(idp_2 - dpint_2))
last_tol1 = tol_1(calc)
last_tol2 = tol_2(calc)
if last_tol1 >= 0.9 && last_tol2 >= 0.9 && last_tol1 < 1 && last_tol2 < 1
disp('force calc is done!!')
break
else
end
end
Another one for simple one idea:
i=0;
act_f=10
while i <= 20
i = i+1;
r = rem(i,3);
if r == 1
disp('its first num')
act_f = act_f + 1
elseif r == 2
disp('its sec num')
act_f = act_f + 2
else
disp('its 3rd num')
act_f = act_f + 3
end
tolerance = act_f/1000
if tolerance >1
break
end
end
댓글 수: 0
답변 (1개)
Voss
2022년 7월 2일
If all the if/elseif blocks have the same structure, which is this:
if last_tol1 < 0.90
act_f = act_f + afl_inc1
elseif last_tol1 > 1
act_f = act_f + afl_dec1
else
act_f = act_f
end
and the only difference is that afl_inc1/afl_dec1 need to be afl_inc2/afl_dec2 or afl_inc3/afl_dec3, etc., depending on the value of rem(calc,N), then you can create arrays of your afl_inc/afl_dec values beforehand and then index into them with an index based on rem(calc,N).
For instance:
N = 4;
afl_inc = [afl_inc1 afl_inc2 afl_inc3 afl_inc4];
afl_dec = [afl_dec1 afl_dec2 afl_dec3 afl_dec4];
calc = -1;
while true
calc = calc + 1;
rem_f = rem(calc,N);
if last_tol1 < 0.90
act_f = act_f + afl_inc(rem_f+1)
elseif last_tol1 > 1
act_f = act_f + afl_dec(rem_f+1)
else
act_f = act_f
end
end
The above will use afl_inc1/afl_dec1 on the 1st iteration, then afl_inc2/afl_dec2 on the 2nd iteration, etc. You can adjust the N and the order as needed.
댓글 수: 3
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!