How to find the average of y over a range of x?
64 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 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 Comments
Voss
on 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))
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!