%% Moving Window Calculation with Tall Array
% Use matlab.tall.blockMovingWindow to calculate the moving mean of airline arrival and departure delays.
%
% Create a datastore for the airlinesmall.csv data set and convert it into a tall array. The data contains information about arrival and departure times of US flights. Extract the ArrDelay and DepDelay variables, which are vectors of flight delays, to create a tall array containing the delays as separate columns.

varnames = {'ArrDelay', 'DepDelay'};
ds = tabularTextDatastore('airlinesmall.csv', 'TreatAsMissing', 'NA', ...
    'SelectedVariableNames', varnames);
tt = tall(ds);
tX = [tt.ArrDelay tt.DepDelay]

%
% Use matlab.tall.blockMovingWindow to calculate the moving mean of the data in the first dimension with a window size of 10. Since windowfcn applies only to single windows of data, you can use the mean function to reduce the windows of data down into a matrix with one row. The blockfcn applies to whole blocks of data, so use the movmean function to calculate the mean of each full window of data in the blocks.

windowfcn = @(info,x) mean(x,1,'omitnan');
blockfcn = @(info,x) movmean(x,info.Window,1,'omitnan','EndPoints','discard');
A = matlab.tall.blockMovingWindow(windowfcn, blockfcn, 10, tX)

%
% Gather a portion of the results into memory.

gather(A(1:10,:))

% Copyright 2018 The MathWorks, Inc.