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

4 ビュー (過去 30 日間)
M Adli Hawariyan
M Adli Hawariyan 2022 年 7 月 5 日
回答済み: Chunru 2022 年 7 月 5 日
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 Center および File ExchangeParallel and Cloud についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by