%% Stack Variables and Output Index Vector
% Create a timetable containing estimated influenza rates per month along the east coast of the United States. Create a different variable for the Northeast, Mid Atlantic, and South Atlantic. Data Source: the Google Flu Trends project (since discontinued). Use a datetime array as row times of the timetable.

Month = datetime(2005,10,1,'Format','MMMM yyyy') + calmonths(0:5);
Month = Month';
NE = [1.1902; 1.3610; 1.5003; 1.7772; 2.1350; 2.2345];
MidAtl = [1.1865; 1.4120; 1.6043; 1.8830; 2.1227; 1.9920];
SAtl = [1.2730; 1.5820; 1.8625; 1.9540; 2.4803; 2.0203];

fluU = timetable(Month,NE,MidAtl,SAtl)

% The variable Month contains data that is constant across the row.
%
% Stack the variables NE, MidAtl, and SAtl into a single variable called FluRate. Name the new indicator variable Region and output an index vector, ifluU, to indicate the correspondence between rows in the input unstacked table, fluU, and the output stacked table, fluS.

[fluS,ifluU] = stack(fluU,1:3,...
                     'NewDataVariableName','FluRate',...
                     'IndexVariableName','Region')

% ifluU(5) is 2. The fifth row in the output table, fluS, contains data from the second row in the input table fluU.
% Copyright 2012-2020 The MathWorks, Inc.