How to find the average of y over a range of x?
36 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Marcella Roth
am 25 Apr. 2022
Kommentiert: Voss
am 26 Apr. 2022
Hi Everyone,
I have a dataset containing two variables, x (time) and y (amplitude). I want to find the average amplitude over a given time range. I'm very much a beginner with MatLab and any help would be appreciated!
0 Kommentare
Akzeptierte Antwort
Voss
am 25 Apr. 2022
% some times, x:
x = 0:0.01:9
% some amplitudes, y:
y = sin(x)
% plot y vs x to visualize (see plot below):
plot(x,y)
% a time range, x_min and x_max:
x_min = 2.2;
x_max = 2.9;
% find the average value of y, where x is within the interval [x_min,x_max]:
y_avg = mean(y(x >= x_min & x <= x_max))
% plot that too:
hold on
plot([x_min x_max],[y_avg y_avg])
2 Kommentare
Voss
am 26 Apr. 2022
You're welcome!
Yes, this will average just the positive numbers within the given range:
% some times, x:
x = 0:0.01:9;
% some amplitudes, y:
y = sin(x);
% a time range, x_min and x_max:
% (pick a range that includes some non-positive y values)
x_min = 2;
x_max = 6;
% find the average value of y, where x is within the interval [x_min,x_max]
% and y is positive:
y_avg = mean(y(x >= x_min & x <= x_max & y > 0))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!