%% Apply Window Function with Multiple Outputs
% Use matlab.tall.movingWindow to apply a function with multiple outputs to windows of data.
%
% Create a tall array from an in-memory random matrix.

X = rand(1000,5);
tX = tall(X)

%
% Create a function that finds the sum, mean, median, and mode of each window of data in the first dimension. Each output needs to have the same size in the first dimension, but the other dimensions can have different sizes. For each window of data, the sum calculation produces a scalar, while the other calculations produce 1-by-N vectors.
% Save the function in your local workspace.
% function [S,mn,mdn,md] = mystats(X)
%   S = sum(X,[2 1]);
%   mn = mean(X,1);
%   mdn = median(X,1);
%   md = mode(X,1);
% end
% Note: This function is included at the end of the example as a local function.
%
% Use matlab.tall.movingWindow to apply the mystats function to the data with a window size of 250. Specify four output arguments to return all of the outputs from mystats. Use the 'EndPoints' name-value pair to discard incomplete windows.

[tS,tmn,tmdn,tmd] = matlab.tall.movingWindow(@mystats, 250, tX, 'EndPoints', 'discard')

%

function [S,mn,mdn,md] = mystats(X)
  S = sum(X,[2 1]);
  mn = mean(X,1);
  mdn = median(X,1);
  md = mode(X,1);
end

% Copyright 2018 The MathWorks, Inc.