# How to calculate circular mean every 8 cells?

2 views (last 30 days)
Yoni Verhaegen -WE-1718- on 3 Jun 2022
Answered: Star Strider on 3 Jun 2022
Hi all,
I have a dataset with wind directions at 3-hourly time steps and I would like to calculate daily average wind directions. I am, however, stuck. I got this (with dir = the wind direction in degrees, 0 to 360):
dir = accumarray(ceil((1:numel(dir))/8)',dir(:),[],@mean);
This, however, does not take into account the circular nature of the values. How can I do this?
I attach a small sample of the data (year - month - day - hour - wind direction):
2020 4 1 0 77.0016556593828
2020 4 1 3 61.6846224380681
2020 4 1 6 31.4624383596130
2020 4 1 9 35.1994739111645
2020 4 1 12 56.7123189304487
2020 4 1 15 337.212660715222
2020 4 1 18 332.720394142200
2020 4 1 21 359.309990882483
2020 4 2 0 1.81332665386765
2020 4 2 3 316.084846935155
2020 4 2 6 297.362308097189
2020 4 2 9 286.447414863766
2020 4 2 12 253.377128455776
2020 4 2 15 272.755841963236
2020 4 2 18 283.300130880562
2020 4 2 21 305.805458428448
2020 4 3 0 303.435585960465
2020 4 3 3 305.408085546923
2020 4 3 6 314.945219141016
2020 4 3 9 301.363197206495
2020 4 3 12 297.344102342364
2020 4 3 15 294.845524112617
2020 4 3 18 301.544742721005
2020 4 3 21 320.320593041074

Star Strider on 3 Jun 2022
One option would be to ‘decompose’ the angles into their ‘x’ and ‘y’ components, calculate the means of those, and then use atan2 to return the result —
D = [2020 4 1 0 77.0016556593828
2020 4 1 3 61.6846224380681
2020 4 1 6 31.4624383596130
2020 4 1 9 35.1994739111645
2020 4 1 12 56.7123189304487
2020 4 1 15 337.212660715222
2020 4 1 18 332.720394142200
2020 4 1 21 359.309990882483
2020 4 2 0 1.81332665386765
2020 4 2 3 316.084846935155
2020 4 2 6 297.362308097189
2020 4 2 9 286.447414863766
2020 4 2 12 253.377128455776
2020 4 2 15 272.755841963236
2020 4 2 18 283.300130880562
2020 4 2 21 305.805458428448
2020 4 3 0 303.435585960465
2020 4 3 3 305.408085546923
2020 4 3 6 314.945219141016
2020 4 3 9 301.363197206495
2020 4 3 12 297.344102342364
2020 4 3 15 294.845524112617
2020 4 3 18 301.544742721005
2020 4 3 21 320.320593041074];
dir = D(:,5);
meandir = @(a) atan2d(mean(sind(a)),mean(cosd(a)));
dir = accumarray(ceil((1:numel(dir))/8)',dir(:),[],meandir)
dir = 3×1
27.0977 -64.0652 -55.1192
dir = wrapTo360(dir) % Mapping Toolbox Function
dir = 3×1
27.0977 295.9348 304.8808
.

### Categories

Find more on Time Series Events in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by