finding the mean based on a specific value in other column

2 views (last 30 days)
How do I find the mean of the temperature at the 6th Month of the year?

Answers (2)

Vilém Frynta
Vilém Frynta on 29 Nov 2022
Edited: Vilém Frynta on 29 Nov 2022
Find months that == 6 with function find(). Then use index of this function to get the temperatures. Then use mean() on these temperatures.
Example:
% Creating random table to work with (similar to your table)
T = table();
T.Day = [1:1:17]';
T.Month = [1 2 3 4 4 5 6 6 6 6 7 8 9 10 11 11 12]';
T.Temperature = randi([29 32],17,1)
T = 17×3 table
Day Month Temperature ___ _____ ___________ 1 1 30 2 2 29 3 3 31 4 4 29 5 4 29 6 5 31 7 6 30 8 6 29 9 6 29 10 6 30 11 7 30 12 8 32 13 9 31 14 10 30 15 11 31 16 11 32
% Find position of all rows where Month == 6
idx = find(T.Month==6)
idx = 4×1
7 8 9 10
% Use idx to get all the temperatures
idxTemperature = T.Temperature(idx)
idxTemperature = 4×1
30 29 29 30
% Calculate mean
meanTemperature = mean(idxTemperature)
meanTemperature = 29.5000
Edit: added an example

Shivraj
Shivraj on 2 Dec 2022
The code is not running

Products

Community Treasure Hunt

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

Start Hunting!