Repeating a sequence of code multiple times in a for loop with random intervals in between

1 次查看(过去 30 天)
I want to repeat the following code 150 times with 500-800 milliseconds in between (i.e. I want the iterations to be spaced apart randomly, with each spacing being between 500 and 800).
rec=@(x) (x)>=0.75
j = (0 : 0.01 : 29);
k = sin(j);
f = rec(k);
[a,b]=find(f~=0);
N=length(b);
s=0.15;
f=double(f);
f(b)=1+s*rand(1,N);
plot(j,f);
axis([0 29 0 2])

采纳的回答

Walter Roberson
Walter Roberson 2020-10-19
rec=@(x) (x)>=0.75;
for counter = 1 : 150
j = (0 : 0.01 : 29);
k = sin(j);
f = rec(k);
[a,b]=find(f~=0);
N=length(b);
s=0.15;
f=double(f);
f(b)=1+s*rand(1,N);
plot(j,f);
axis([0 29 0 2])
drawnow
delay = 500+300*rand();
pause(delay/1000);
end
  2 个评论
Emma Plater
Emma Plater 2020-10-20
Great, thank you!
How do I then store all iteration loop outputs in a single row? Right now only the result of the last iteration is saved and I'd like to be able to plot all 150 iterations continuously in a single graph.
Walter Roberson
Walter Roberson 2020-10-20
You cannot store delays. Delays are something that happen in real time. MATLAB does not have any way to allow you to submit a list of points to plot with built-in delays at particular points in the output.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by