How to calculate the running average of points falling inside the circle ??? and plotting the running average ??

hi everyone,
I need some help in calculating the average of points falling inside the circle,the running average sould start from the first point until 1000. and how can i plot the running average ?? i need to Plot a black horizontal line at y =pi from 1 to 1000. also i do not know if the code is right or not so please help.
the code: clc;clear % plotting the square s=rng(171172); xy = rand(1000,2);
plot(xy(:,1),xy(:,2),'b.') hold on % ploting the circle t = linspace(0,2*pi); plot(.5+.5*cos(t),.5+.5*sin(t),'r-') axis square r = hypot(xy(:,1)-.5,xy(:,2)-.5); plot(xy(r<=.5,1),xy(r<=.5,2),'ro') plot(xy(r>=.5,1),xy(r>=.5,2),'bs')
thanks,

Réponses (1)

Hello Mohammed,
If I am understanding you correctly, you would like to figure out the average of all the points within the circle, but as more of a cumulative average. So the first average would just be the first point in the circle, the second would be the average of the first two points, the third would be the average of the first three points.
That's not too bad to do. You can use the logical indexing you're already using for the plot to extract the points within the circle. Then you can calculate the cumulative summation of all those points, and divide by the number of points summed up to that point (pun intended) to get the average.
xyInCircle = xy(r < 0.5, :); % Could make it 1:1000 if you want limits
csXYInCircle = cumsum(xyInCircle); % Defaults to column-wise
indexes = (1:size(csXYInCircle, 1)).';
avgXYInCircle = csXYInCircle./([indexes indexes]);
plot(avgXYInCircle(:,1),avgXYInCircle(:,2),'-g')
I'm not sure what you mean by plotting that line at y = pi. You can plot a line from x = 1 to x = 1000 at y = pi with:
plot([1 1000], [pi pi], '-k')
but I feel like that's not what you want, since it will extend far outside the current plot. Maybe you just meant x = 0 to x = 1?
I hope this helps!
-Cam

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by