2016-12-10 42 views
1

我有一个问题,我希望我会在那里找到帮助。 这是我的示例代码。它是我算法的唯一部分。想象一下,在这个方程中,点是如何移动的,我需要用两个变量和点表示函数的轮廓。因为我有比抛物线函数更难的功能,所以方程式比我需要的要长。为此,我在循环之前移动轮廓绘图。但我有问题。我需要总是显示计数,只显示i-loop,我的解决方案不起作用。请帮帮我!MATLAB - 在循环之后向另一个绘图添加绘图

[R S] = meshgrid(-5:0.1:5, -5:0.1:5); 

figure 
contour(R, S, R.^2 + S.^2, 5); 
axis([-5,5,-5,5]) 
axis square 
hold on 

for i=1:50 
    a = 0; 
    b = 1:2 
    B = repmat(b,5,1) 
    A = unifrnd(a,B) 
    x = A(1:5,1); 
    y = A(1:5,2); 

    scatter(x,y,'fill') 
    hold off 
    pause(0.5) 
end 

回答

0

你应该把手存储到您的scatter情节和简单地更新它的XDataYData特性,而不是每次都破坏了剧情对象

[R S] = meshgrid(-5:0.1:5, -5:0.1:5); 

figure 
contour(R, S, R.^2 + S.^2, 5); 
axis([-5,5,-5,5]) 
axis square 
hold on 

% Create a scatter plot and store the graphics handle so we can update later 
hscatter = scatter(NaN, NaN, 'fill'); 

for i=1:50 
    a = 0; 
    b = 1:2 
    B = repmat(b,5,1) 
    A = unifrnd(a,B) 
    x = A(1:5,1); 
    y = A(1:5,2); 

    % Update the X and Y positions of the scatter plot 
    set(hscatter, 'XData', x, 'YData', y); 

    pause(0.5) 
end 
+0

谢谢,它的工作原理:)有一个愉快的一天 –