How to determine the mean of certain y values ?

1 view (last 30 days)
This may sound like a genuinely easy question, but I'm quite a newbie.
So far I imported values from a .txt file (A = readtable) and seperated: x = A{:,1}; and y = A{:,3}; in order to plot it. For whatever reason I'd like the mean of some y values to be computed. I tried: ind = y>= ... & <= ... followed by mean(ind), but it didn't work properly. It'd be most kind if you could show me how to perform this mere calculation correctly.
Kind Regards.

Accepted Answer

Star Strider
Star Strider on 16 May 2020
It would be helpful to see all the relevant code.
However on the basis of what you posted, taking the mean of ‘ind’ is going to take the mean of the logical vector, so the result is going to be between 0 and 1, since logical values become numeric when used in other numeric calculations. The calculation of ‘ind’ may also not be correct, although the full code for it is not presented.
Try this:
A = sortrows(rand(25,3),1); % Create ‘A’
x = A(:,1);
y = A(:,2);
ind = y >= 0.4 & y <= 0.6;
yindmean = mean(y(ind));
That should do what you want.
  2 Comments
Niklas Kurz
Niklas Kurz on 17 May 2020
Edited: Niklas Kurz on 17 May 2020
Thank you for your Answere! I figured
mean(y(1:120))
also works just fine. Kind regards.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!