Clear Filters
Clear Filters

How to modify this code for calculation seasonal values for each year (not all years)

2 views (last 30 days)
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:
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.

Accepted Answer

Ameer Hamza
Ameer Hamza on 3 Apr 2020
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
  6 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!