Borrar filtros
Borrar filtros

How to assign a mean of a variable to every year ?

2 visualizaciones (últimos 30 días)
HADJ MOHAMMED
HADJ MOHAMMED el 3 de Nov. de 2023
Respondida: Image Analyst el 5 de Nov. de 2023
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:

Respuestas (3)

KSSV
KSSV el 3 de Nov. de 2023
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
Stephen23 el 3 de Nov. de 2023
Editada: Stephen23 el 3 de Nov. de 2023
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)
T = 10×3 table
DateYear SeverityCode SST ________ ____________ _____ 2016 3 26.33 2016 3 26.77 2010 0 29.05 2008 2 28.61 2016 3 26.71 2016 3 25.37 2009 1 27.1 2008 1 27.95 2007 2 28.62 2010 0 -11
G = groupsummary(T,'DateYear','mean','SST')
G = 5×3 table
DateYear GroupCount mean_SST ________ __________ ________ 2007 1 28.62 2008 2 28.28 2009 1 27.1 2010 2 9.025 2016 4 26.295
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
T = 10×3 table
DateYear SeverityCode SST ________ ____________ _____ 2016 3 26.33 2016 3 26.77 2010 0 29.05 2008 2 28.61 2016 3 26.71 2016 3 25.37 2009 1 27.1 2008 1 27.95 2007 2 28.62 2010 0 NaN
G = groupsummary(T,'DateYear','mean','SST')
G = 5×3 table
DateYear GroupCount mean_SST ________ __________ ________ 2007 1 28.62 2008 2 28.28 2009 1 27.1 2010 2 29.05 2016 4 26.295

Image Analyst
Image Analyst el 5 de Nov. de 2023
Like @Stephen23 said, you can use groupsummary. Other options are splitapply and grpstats. Let us know if you can't figure it out.

Categorías

Más información sobre Directed Graphs en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by