Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
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 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
(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 Kommentare
Antworten (2)
John D'Errico
am 30 Apr. 2018
Bearbeitet: John D'Errico
am 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 Kommentare
John D'Errico
am 1 Mai 2018
Bearbeitet: John D'Errico
am 1 Mai 2018
N = length(X);
WTP? If you have the data, then you can determine the length of the data.
Jon
am 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 Kommentare
Walter Roberson
am 2 Mai 2018
Sorry, I do not use Excel on Windows. But see also https://www.mathworks.com/matlabcentral/answers/275384-xlswrite-error-worksheet-could-not-be-activated
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!