How to find the average of y over a range of x?
64 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marcella Roth
el 25 de Abr. de 2022
Comentada: Voss
el 26 de Abr. de 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 comentarios
Respuesta aceptada
Voss
el 25 de Abr. de 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 comentarios
Voss
el 26 de Abr. de 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))
Más respuestas (0)
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!