2016-12-10 62 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

謝謝,它的工作原理:)有一個愉快的一天 –