streamlining or sliming endless if-elseif of rem function, How?

4 次查看(过去 30 天)
Hello, i am currently need to streamlining/sliming if-elseif of rem function, because it will be endless
so here is the problem.
i have to create if-elseif of rem function in slimmer way because the N value will be much higher (20 to 50 i think) so i have to make the statement if-esleif like this:
  • if rem_f == 1
  • elseif rem_f == 2
  • ....
  • elseif rem_f == 49
  • else %rem_f == 50
statement inside the if-elseif rem_f is identical. here's the code. please kindly give me some guidance of this problem.
much love.
%%%%%%%%%%%%%%%%%%%%%%%%%% INPUT DATA
N =3 % 3 varieties of foce calc, or/and initial forces
m_tol = zeros(1,N)
for bbm=1:N
m_tol(1,bbm)=m_tol(1,bbm)+ 0.1*bbm
end
act_f = (zeros(12,1)+1)*10
afl_inc = (zeros(12,N)+1)*20
afl_inc(1,:) = afl_inc(1,:) + [ 0 10 20 ]
afl_dec = (zeros(12,N)+1)*30
afl_dec(1,:) = afl_dec(1,:) + [ 0 10 20 ]
%%%%%%%%%%%%%%%%%%%%%%%%%%% CORE CODE LINE
calc = 0;
for bbb = 1:4
rem_f = rem(calc,N)+1
calc = calc + 1;
%%
if rem_f == 1
if m_tol(rem_f) < 0.90
act_f = act_f + afl_inc(:,rem_f)
elseif m_tol(rem_f) > 1
act_f = act_f + afl_dec(:,rem_f)
else
act_f = act_f
end
%%
elseif rem_f == 2
if m_tol(rem_f) < 0.90
act_f = act_f + afl_inc(:,rem_f)
elseif m_tol(rem_f) > 1
act_f = act_f + afl_dec(:,rem_f)
else
act_f = act_f
end
%%
else %rem_f == 3
if m_tol(rem_f) < 0.90
act_f = act_f + afl_inc(:,rem_f)
elseif m_tol(rem_f) > 1
act_f = act_f + afl_dec(:,rem_f)
else
act_f = act_f
end
end
% there should be calculation for generating m_tol each loop, but i leave
% it for later
%edit: m_tol 1 2 3 change to value of rem_f
end

采纳的回答

Chunru
Chunru 2022-7-5
N =3 % 3 varieties of foce calc, or/and initial forces
m_tol = zeros(1,N)
for bbm=1:N
m_tol(1,bbm)=m_tol(1,bbm)+ 0.1*bbm
end
act_f = (zeros(12,1)+1)*10
afl_inc = (zeros(12,N)+1)*20
afl_inc(1,:) = afl_inc(1,:) + [ 0 10 20 ]
afl_dec = (zeros(12,N)+1)*30
afl_dec(1,:) = afl_dec(1,:) + [ 0 10 20 ]
%%%%%%%%%%%%%%%%%%%%%%%%%%% CORE CODE LINE
calc = 0;
for bbb = 1:4
rem_f = rem(calc,N)+1
calc = calc + 1;
% One if-else should be sufficient
if m_tol(rem_f) < 0.90
act_f = act_f + afl_inc(:,rem_f)
elseif m_tol(rem_f) > 1
act_f = act_f + afl_dec(:,rem_f)
else
act_f = act_f
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Biological and Health Sciences 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by