streamlining or sliming endless if-elseif of rem function, How?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
M Adli Hawariyan
el 5 de Jul. de 2022
Respondida: Chunru
el 5 de Jul. de 2022
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
0 comentarios
Respuesta aceptada
Chunru
el 5 de Jul. de 2022
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Biological and Health Sciences en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!