%% Calculate Mean Values of Tall Vectors
% Create a tall table, extract two tall vectors form the table, and then calculate the mean value of each vector.
%
% Create a tall table for the airlinesmall.csv data set. The data contains information about arrival and departure times of US flights. Extract the ArrDelay and DepDelay variables, which are vectors of arrival and departure delays.

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

%
% In the first stage of the algorithm, calculate the sum and element count for each block of data in the vectors. To do this you can write a function that accepts two inputs and returns one output with the sum and count for each input. This function is listed as a local function at the end of the example.
% function bx = sumcount(tx,ty)
%   bx = [sum(tx) numel(tx) sum(ty) numel(ty)];
% end
% In the reduction stage of the algorithm, you need to add together all of the intermediate sums and counts. Thus, matlab.tall.reduce returns the overall sum of elements and number of elements for each input vector, and calculating the mean is then a simple division. For this step you can apply the sum function to the first dimension of the 1-by-4 vector outputs from the first stage.

 reducefcn = @(x) sum(x,1);
 s = matlab.tall.reduce(@sumcount,reducefcn,tX,tY)
 s = gather(s)

% The first two elements of s are the sum and count for tX, and the second two elements are the sum and count for tY. Dividing the sums and counts yields the mean values, which you can compare to the answer returned by the mean function.

 my_mean = [s(1)/s(2) s(3)/s(4)]
 m = gather(mean([tX tY]))

%
%% Local Functions
% Listed here is the sumcount function that matlab.tall.reduce calls to calculate the intermediate sums and element counts.

function bx = sumcount(tx,ty)
  bx = [sum(tx) numel(tx) sum(ty) numel(ty)];
end

%
% Copyright 2019 The MathWorks, Inc.