如何在Matlab图的每一代for循环中创建影片 [英] How to create movies on each generation of a for loop in Matlab plot
问题描述
我想将球面函数绘制为曲面或等高线图,并通过叠加高斯分布所生成数据的球面函数来评估最佳个体的位置和适合度值.情节会在每一代发生变化,所以我会看电影.另外,在另一张图上,我想绘制球面函数的轮廓,该轮廓与高斯分布所产生的总体进行叠加,保留的适合个体用红色上色.这代代相传应该再拍一部电影.
I want to plot the sphere function as a surface or a contour plot, and the position and fitness value of the best individual which are evaluated by a sphere function of data generated from a Gaussian distribution superimposed with it. The plot will change in each generation so i get a movie. Also, on another figure, i want to plot the contour of the sphere function superimposed with the entire population generated from Gaussian distribution, with the retained fit individuals colored in red. This from generation to generation should give another movie.
这基本上是分配算法(EDA)估计的一种实现.有人知道该怎么做吗?
This is basically an implementation of estimation of distribution algorithm(EDA). Anyone know how to do this?
编辑
EDIT
K= 4
for l = 1 : K
contour(X,Y,ph);
hold on
plot(bestId, 'rx');
end
上面的代码应叠加等高线图和BestId.对于l的每个循环,都会生成bestId并将其与等高线图叠加. BestInd的这一代应针对每个l进行.但是从1到4的bestId世代中,有一个是其中最好的世代,我们应该在四次后将其与等高线图叠加起来.现在,我希望这一代成为一部从1到4的到达迭代的电影,以便我可以看到bestId是如何生成的,直到达到best(最佳)为止. 这就是我在Matlab中看电影的意思.关于如何做到这一点的任何想法?
The above code should superimpose contour plot and BestId. For each loop of l, a bestId is generated and superimposed with the contour plot. This generation of BestInd should take place for each l. but among the generations of bestId from 1 to 4, there is one which is the best of them and we should get that after four generation which is superimposed with the contour plot. Now i want this generation to be a movie for reach iteration from 1 to 4, so that i can see how the bestId are generated untill the best(optimal) one is achieved. this is what i meant by movie in matlab. Any idea on how this could be done?
推荐答案
要创建反映人物变化的电影,我使用类avifile
和功能getframe()
和addframe()
To create movie reflecting changes in figures, I am using combination of the class avifile
and functions getframe()
and addframe()
这是一个例子
aviobj = avifile('example.avi','compression','None');
t = linspace(0,2.5*pi,40);
fact = 10*sin(t);
fig=figure;
[x,y,z] = peaks;
for k=1:length(fact)
h = surf(x,y,fact(k)*z);
axis([-3 3 -3 3 -80 80])
axis off
caxis([-90 90])
F = getframe(fig);
aviobj = addframe(aviobj,F);
end
close(fig);
aviobj = close(aviobj);
您可以在此处找到更多信息
You can find more info here
http://www.mathworks.nl/help/matlab/ref/avifile.html
http://www.mathworks.nl/help/matlab/ref/movie.html
http://www.math.canterbury.ac .nz/〜c.scarrott/MATLAB_Movies/movies.html
----------------- 讨论后在评论中进行编辑 ---------------- -
-----------------Edit after the discussion in the comments------------------
pm89
在注释中建议了另一种方式. VideoWriter类似乎更现代并且是最新的.使用示例可在下面的页面结尾找到
pm89
suggested another way in the comments. The VideoWriter class seems more modern and up to date. The example of use can be found at the end of the page below
http://www.mathworks.nl/help/matlab/ref/videowriterclass.html
这篇关于如何在Matlab图的每一代for循环中创建影片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!