Info

This question is closed. Reopen it to edit or answer.

Dear Sirs, would you please help me and tell me how can I plot the average value of the previous y, while we have thousands (x, y) versus the current X?

1 view (last 30 days)
(xn, yn), yn=(sigma (i=1) to (i=n) [yn])/n
output(xn, yn=average of previous y and the current y)
We have thousands of dots like (xn, yn), but the number of our data is accidental and it changes by time of sampling. So, there is a necessity to have dynamic code. I think it is not so difficult. Before anything, I am so grateful for your helping. Thank you so much.
  4 Comments
Hadi Naderiallaf
Hadi Naderiallaf on 1 May 2018
Dear Walter, actually, I need a scatter plot in the output including these points: (x1, y1), (x2, average y1 and y2), (x3, average y1, y2 and y3), ........, (xn, average y1, y2, ......, yn). The output should be a graph including thousands of points in the x-y plane. Thank you for your help. I am looking forward to hearing from you.
Hadi Naderiallaf
Hadi Naderiallaf on 1 May 2018
Edited: Hadi Naderiallaf on 1 May 2018
Dear Walter, another point is that all of my data are in text file format. I can also put them in Excel file. Would you please tell me how can I refer the code to the text file or excel file? Thank you so much.

Answers (2)

John D'Errico
John D'Errico on 30 Apr 2018
Edited: John D'Errico on 30 Apr 2018
If you just have a vector X and a vector Y, and you want to plot the average of all preceding Y as a function of X, this is trivial and easy to do efficiently.
% some garbage data
N = 10000;
X = rand(1,N);
Y = X + randn(size(X));
[X,sorttags] = sort(X);
Y = Y(sorttags);
% The average of all preceding Y, including the current Y
Ymean = cumsum(Y)./(1:N);
plot(X,Ymean,'.')
WTP?
Or, if you don't want to include the current element in that average...
plot(X(2:end),Ymean(1:end-1),'.')
Or, if you just want the average of the last 10 elements...
Ymean10 = conv(Y,ones(1,10)/10,'valid');
plot(X(10:end),Ymean10,'.')
It would be slightly more difficult if you wanted the mean of only those elements within a specified distance in X less than the current point, because then the number of elements in those means will vary.
  3 Comments
Hadi Naderiallaf
Hadi Naderiallaf on 1 May 2018
Edited: Hadi Naderiallaf on 1 May 2018
Dear Walter and John, thank you so much for your consideration. The point here is that the number of data is accidental. Thus, in the following code you used N=10000, can not be defined previously before data processing. What should we do with accidental number of points?

Jon
Jon on 30 Apr 2018
If you do not want to keep all of the y's in memory and are only interested in the average of all of the y's up to the current sample (N+1) you can do this recursively as: ybar = n/(n+1)*ybar + y/(n+1)
Where ybar is the next value of the average, and n is the total number of point obtained so far. So in this case you just have to keep track of two scalar values, the count n, and the last value of the average, ybar
  12 Comments
Hadi Naderiallaf
Hadi Naderiallaf on 2 May 2018
Thank you so much dear Walter. Now, it is working. The problem was in Excel file the cell with zero values should be removed first. Thank you so much for your help.

Tags

Community Treasure Hunt

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

Start Hunting!