Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
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 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
(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 commentaires
Réponses (2)
John D'Errico
le 30 Avr 2018
Modifié(e) : John D'Errico
le 30 Avr 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 commentaires
John D'Errico
le 1 Mai 2018
Modifié(e) : John D'Errico
le 1 Mai 2018
N = length(X);
WTP? If you have the data, then you can determine the length of the data.
Jon
le 30 Avr 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 commentaires
Walter Roberson
le 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
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!