Running average from vector of data
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
shobhit mehrotra
am 8 Apr. 2015
Bearbeitet: Image Analyst
am 8 Apr. 2015
Hi, I have a vector A A = (1 ,3 ,4 -2, 5 ,6 8, 9, -4, -2)
I want to create a vector with the running average such that
B = (A1, (A1+A2)/2, (A1+A2+A3)/3, ....) then plot(B)
Thanks!
0 Kommentare
Akzeptierte Antwort
James Tursa
am 8 Apr. 2015
Bearbeitet: James Tursa
am 8 Apr. 2015
x = 1:numel(AA);
B = cumsum(AA)./x;
plot(x,B);
0 Kommentare
Weitere Antworten (1)
Image Analyst
am 8 Apr. 2015
Bearbeitet: Image Analyst
am 8 Apr. 2015
If you have the Curve Fitting Toolbox, try smooth: http://www.mathworks.com/help/curvefit/smooth.html?searchHighlight=smooth
Otherwise, use conv() (twice) and plot().
% Create sample data.
signal = randi(9, 1, 5)
% Make a moving window (kernel) to do the counting.
kernel = [1, 1, 1];
% Count the number of elements in the moving window.
counts = conv(ones(1, length(signal)), kernel, 'full')
% Sum the signal in the moving window.
sums = conv(signal, kernel, 'full')
% Divide the sums by the counts to get the average.
movingAverage = sums ./ counts
plot(movingAverage, 'b-', 'LineWidth', 3);
grid on;
Sample data:
signal =
3 2 8 2 1
counts =
1 2 3 3 3 2 1
sums =
3 5 13 12 11 3 1
movingAverage =
3.0000 2.5000 4.3333 4.0000 3.6667 1.5000 1.0000
0 Kommentare
Siehe auch
Kategorien
Mehr zu Signal Generation and Preprocessing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!