Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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 回表示 (過去 30 日間)
Hadi Naderiallaf
Hadi Naderiallaf 2018 年 4 月 30 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
(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 件のコメント
Hadi Naderiallaf
Hadi Naderiallaf 2018 年 5 月 1 日
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 2018 年 5 月 1 日
編集済み: Hadi Naderiallaf 2018 年 5 月 1 日
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.

回答 (2 件)

John D'Errico
John D'Errico 2018 年 4 月 30 日
編集済み: John D'Errico 2018 年 4 月 30 日
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 件のコメント
Hadi Naderiallaf
Hadi Naderiallaf 2018 年 5 月 1 日
編集済み: Hadi Naderiallaf 2018 年 5 月 1 日
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?
John D'Errico
John D'Errico 2018 年 5 月 1 日
編集済み: John D'Errico 2018 年 5 月 1 日
N = length(X);
WTP? If you have the data, then you can determine the length of the data.

Jon
Jon 2018 年 4 月 30 日
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 件のコメント
Hadi Naderiallaf
Hadi Naderiallaf 2018 年 5 月 2 日
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.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by