How to use if inside loop
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
Andrei Bobrov
2017-6-6
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);
类别
在 帮助中心 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!