How to assign a mean of a variable to every year ?
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have Sea surface temperature per year but I have many values for the same year. How to calculate the mean of SST for every year?
Thank you.
This is the table:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1528366/image.png)
0 个评论
回答(3 个)
KSSV
2023-11-3
T = readtable(myfile) ;
[c,ia,ib] = unique(T.(1)) ; % gewt unique values of years
N = length(c) ;
iwant = zeros(N,2) ;
for i = 1:N
iwant(i,1) = c(i) ;
iwant(i,2) = mean(T.(3)(ib==i)) ;
end
Stephen23
2023-11-3
编辑:Stephen23
2023-11-3
The simple and efficient MATLAB approach:
DateYear = [2016;2016;2010;2008;2016;2016;2009;2008;2007;2010];
SeverityCode = [3;3;0;2;3;3;1;1;2;0];
SST = [26.33;26.77;29.05;28.61;26.71;25.37;27.1;27.95;28.62;-11];
T = table(DateYear,SeverityCode,SST)
G = groupsummary(T,'DateYear','mean','SST')
Note that if the value -11 represents invalid data then those lines will need to be modified e.g. removed from the table or -11 replaced with NaN:
T.SST(T.SST==-11) = NaN
G = groupsummary(T,'DateYear','mean','SST')
Image Analyst
2023-11-5
Like @Stephen23 said, you can use groupsummary. Other options are splitapply and grpstats. Let us know if you can't figure it out.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Directed Graphs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!