Plotting too many points

12 views (last 30 days)
Emmers
Emmers on 5 Feb 2015
Edited: Nicolás Casaballe on 5 Jul 2018
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

Answers (1)

Nicolás Casaballe
Nicolás Casaballe on 5 Jul 2018
Edited: Nicolás Casaballe on 5 Jul 2018
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 ).

Community Treasure Hunt

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

Start Hunting!