How to use if inside loop
Afficher commentaires plus anciens
I'm working on matlab code for counting number of reflection, here is my code
RY=30;
L_B=40;
L_S=20;
% Single Reflection Left Side
if RY < 2*L_B
R1L=1 % 1
elseif 2*L_B<RY && RY< 2*(L_B+L_S)
R1L=0 % 3
elseif 2*(L_B+L_S) < RY && RY< 2*(2*L_B+L_S)
R1L=1 % 5
elseif 2*(2*L_B+L_S) < RY && RY< 2*(2*L_B+2*L_S)
R1L=0 % 7
elseif 2*(2*L_B+2*L_S) < RY && RY< 2*(3*L_B+2*L_S)
R1L=1 % 9
elseif 2*(3*L_B+2*L_S) < RY && RY< 2*(3*L_B+3*L_S)
R1L=0 % 11
elseif 2*(3*L_B+3*L_S) < RY && RY< 2*(4*L_B+3*L_S)
R1L=1 % 13
else
R1L=0
end
% Single Reflection Right Side
if RY < 2*L_B
R1R=1 % 2
elseif 2*L_B<RY && RY< 2*(L_B+L_S)
R1R=0 % 4
elseif 2*(L_B+L_S) < RY && RY< 2*(2*L_B+L_S)
R1R=1 % 6
elseif 2*(2*L_B+L_S) < RY && RY< 2*(2*L_B+2*L_S)
R1R=0 % 8
elseif 2*(2*L_B+2*L_S) < RY && RY< 2*(3*L_B+2*L_S)
R1R=1 % 10
elseif 2*(3*L_B+2*L_S) < RY && RY< 2*(3*L_B+3*L_S)
R1R=0 % 12
elseif 2*(3*L_B+3*L_S) < RY && RY< 2*(4*L_B+3*L_S)
R1R=1 % 14
else
R1R=0
end
Total=R1L+R1R
When it is executed, Total=2
However, I want to change variable RY to from 1 to 200 with step=1 so the size of Total is 200x1 How I supposed to do with looping?
Réponse acceptée
Plus de réponses (1)
Andrei Bobrov
le 6 Juin 2017
RY = 1:200;
L_B = 40;
L_S = 20;
LL = [L_B;L_S];
r = repelem((1:3)',2);
lgt = 2*toeplitz([0;r;4],[0 0])*[L_B;L_S];
lgt(1) = -inf;
lgt = [lgt;inf];
Total = 2*rem(discretize(RY,lgt),2);
Catégories
En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!