![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206961/image.png)
Why do we set r=r+1 when (x(t)-rangeX(r))*(x(t)-rangeX(r+1))>0 in the uniform variables code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
yang-En Hsiao
am 5 Mär. 2019
Kommentiert: darova
am 6 Mär. 2019
a=3;
b=7;
T=10^6;
BinW=0.01;
x=zeros(1,T);
rangeX=3:BinW:7;
Count=zeros(1,length(rangeX)-1);
for t=1:T
u=rand(1);
x(t)=a+(b-a)*u
r=1;
while (x(t)-rangeX(r))*(x(t)-rangeX(r+1))>0
r=r+1;
end
Count(r)=Count(r)+1;
end
bar(rangeX(1:length(rangeX)-1),Count);
axis([2.5 7.5 0 3000]);
I get this code from the class,and this code is about the uniform variables,in this code,i have some lines that i don't understand ,i hope someone can explain them to me.
1.
Count=zeros(1,length(rangeX)-1);
why do we create the 1 by length(rangeX)-1 zero matrix first,but not 1 by length(rangeX) zero matrix ?
2.
while (x(t)-rangeX(r))*(x(t)-rangeX(r+1))>0
r=r+1;
end
I don't understand the meaning of these three lines code,why do we set r=r+1 when (x(t)-rangeX(r))*(x(t)-rangeX(r+1))>0
0 Kommentare
Akzeptierte Antwort
darova
am 5 Mär. 2019
while (x(t)-rangeX(r))*(x(t)-rangeX(r+1))>0
is the cheking if x(t) is not in ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206961/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/206961/image.png)
its the same as:
while ~( rangeX(r) < x(t) && x(t) < rangeX(r+1) )
2 Kommentare
darova
am 6 Mär. 2019
You shoudn't, just showed you an example.
if x(t) is not between that range we r = r+1 (we examine another range). And when x(t) is IN the range we quit from while loop.
![Untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207059/Untitled.png)
In another words: we searching range for x(t)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!