Plotting too many points
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello all, I am trying to put two scatter plots together on one graph, but when I use the hold on function, it plots more than the desired number of points for the second one. How can I plot only the amount I need for the second one? Here is my code:
for N = [0:100]
x = 2.*(rand(N,1)-0.5) - 0.4;
y = 2.*(rand(N,1)-0.5);
scatter(x,y,'r')
axis([-1 1 -1 1]);
pointsincircle1 = find(sqrt(x.^2+y.^2) < 0.6);
end
points1 = length(pointsincircle1);
Areacircle1 = (points1/100)*4
hold on
for N = [0:100]
x1 = 2.*(rand(N,1)-0.5) + 0.4;
y1 = 2.*(rand(N,1)-0.5);
scatter(x1,y1,'b')
axis([-1 1 -1 1]);
pointsincircle2 = find(sqrt(x1.^2+y1.^2) < 0.6);
end
points2 = length(pointsincircle2);
Areacircle2 = (points2/100)*4
댓글 수: 0
답변 (1개)
Nicolás Casaballe
2018년 7월 5일
편집: Nicolás Casaballe
2018년 7월 5일
Hello. Not certain about your intention with this code, but it would seem that you are using a for loop where you don't need to (the way you implemented it, you'll end up with N*(N+1)/2 = 5050 points).
Maybe you were trying to do the following?
% for N = [0:100] % for-loop probably not required
N = 100;
x = 2.*(rand(N,1)-0.5) - 0.4;
y = 2.*(rand(N,1)-0.5);
scatter(x,y,'r') % without hold on, this line clears the previous scatter in the for-loop
axis([-1 1 -1 1]);
pointsincircle1 = find(sqrt(x.^2+y.^2) < 0.6); % review this line?
% end % from the for-loop, not required
%
points1 = length(pointsincircle1);
Areacircle1 = (points1/100)*4
hold on
% for N = [0:100] % for-loop probably not required
x1 = 2.*(rand(N,1)-0.5) + 0.4;
y1 = 2.*(rand(N,1)-0.5);
scatter(x1,y1,'b') % if hold is on, this would keep adding points in a for-loop, N = 0, 1, 2, ...
axis([-1 1 -1 1]);
pointsincircle2 = find(sqrt(x1.^2+y1.^2) < 0.6); % review this line?
% end % from the for-loop
%
points2 = length(pointsincircle2);
Areacircle2 = (points2/100)*4
I also suggest you review your notes about what you are trying to compute (note that your x and x1 are not centered around 0 ).
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!