How do you determine the average values in second column of an excel data corresponding to a particular range of values in first column ?
Show older comments
I have an excel file which containes more than 300000 in both column a and b. If the values in first column is less than 1 ,I need to average the corrosponding values in the second column and then increasing the range to 1 to 2 and so on up to 30..Any help would be greatly appreciated. Thank you.
My file looks like this
2.2060 0.3120
2.2140 0.3138
2.2180 0.3146
2.2260 0.3164
3.8920 0.2611
3.9000 0.259
3.9070 0.2571
3.8840 0.2632
3.9600 0.2431
4.0010 0.2322
If the values in first column is less than 1 ,I need to average the corrosponding values in the second column and then increasing the range to 1 to 2 and so on up to 30..Any help would be greatly appreciated. Thank you.
Accepted Answer
More Answers (1)
Nicolas B.
on 21 Oct 2019
For that situation, the easiest way is to select data based on a criteria. For example, if your table is named t:
indRows = t(:,1) < 1; % get row indexes for values below 1
myMean = mean(t(indRows, 2)); % compute the mean
Of course, you can also merge the 2 line of codes to avoid using indRows.
2 Comments
Duphrin
on 21 Oct 2019
Nicolas B.
on 21 Oct 2019
Then, I think that would be the easiest solution to simply loop with your criterias. I could imagine solutions based on the function discretize(), but it wouldn't avoid a loop:
mylim = [-inf, 1:30, inf]; % list of your limits
y = discretize(t, mylim); % you get the indexes
m = NaN(size(myLim)); % your output vector
for n = 1:numel(m) % loop to get the mean value
m(n) = mean(t(y == n), 'omitnan');
end
Categories
Find more on Matrix Indexing 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!