How to modify this code for calculation seasonal values for each year (not all years)
3 次查看(过去 30 天)
显示 更早的评论
I have a 1x3 cell contains three 360x3 tables. For every table, I want to calculate the average of rrr24 for each season and write them on the 5th column. For example:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/281529/image.png)
I have a code as you can see below, but it calculates the average of each season for all 30 years (not yearly), and didn't write averages on the 5th column too.
I would like just a new 1x3 cell with tables that have the 5th column as I described above.
meanTableDataArray = cell(length(CELL),4);
for j = 1:length(CELL)
sampleTableData = CELL{j};
sampleTableData.month = month(sampleTableData.dates);
sampleTableData.seasons = floor(sampleTableData.month ./3);
sampleTableData.seasons(sampleTableData.seasons ==4 ) = 0;
sampleTableData.seasons = categorical(sampleTableData.seasons, [0 1 2 3], ["Spr", "Sum", "Aut", "Win"]);
[group, mean_table] = findgroups(sampleTableData(:, 'seasons'));
mean_table.rrr24 = splitapply(@mean, sampleTableData.rrr24, group);
for k = 1:4
meanTableDataArray{j,k} = mean_table(k,:); % extracts as a table
end
end
Thank you.
采纳的回答
Ameer Hamza
2020-4-3
Try this
data = load('CELL.mat');
CELL = data.CELL;
month_names = month(datetime(1,1,1):calmonths(1):datetime(1,12,1), 'name');
seasons = ["Spr", "Sum", "Aut", "Win"];
for i=1:numel(CELL)
CELL{i}.grid_name = [];
CELL{i}.month = month_names(month(CELL{i}.dates))';
CELL{i}.seasons = seasons(floor((month(CELL{i}.dates)+2)/3))';
grps = findgroups(findgroups(year(CELL{i}.dates), CELL{i}.seasons));
avg_value = accumarray(grps, CELL{i}.rrr24, [], @mean);
CELL{i}.Average = avg_value(grps);
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!