필터 지우기
필터 지우기

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

조회 수: 1 (최근 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 CenterFile Exchange에서 Parallel and Cloud에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by