| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Statistics Toolbox |
| Contents | Index |
| Learn more about Statistics Toolbox |
Stack data from multiple variables into single variable
tall = stack(wide,datavar)
[tall,iwide] = stack(wide,datavar)
tall = stack(wide,datavar,Parameter,value)
tall = stack(wide,datavar) converts a wide-format dataset array into a tall-format array, by stacking multiple variables in wide into a single variable in tall. In general, tall contains fewer variables but more observations than wide.
datavar specifies a group of m data variables in wide. stack creates a single data variable in tall by interleaving their values, and if wide has n observations, then tall has m-by-n observations. In other words, stack takes the m data values from each observation in wide and stacks them up to create m observations in tall. datavar is a positive integer, a vector of positive integers, a variable name, a cell array containing one or more variable names, or a logical vector. stack also creates a grouping variable in tall to indicate which of the m data variables in wide each observation in tall corresponds to.
stack copies the remaining variables from wide to tall without stacking, by replicating each of their values m times. These variables are typically grouping variables. Because their values are constant across each group of m observations in tall, they identify which observation in wide an observation in tall came from.
[tall,iwide] = stack(wide,datavar) returns an index vector iwide indicating the correspondence between observations in tall and those in wide. stack creates tall(j,:) using wide(iwide(j),datavars).
For more information on grouping variables, see Grouping Variables.
tall = stack(wide,datavar,Parameter,value) uses the following parameter name/value pairs to control how stack converts variables in wide to variables in tall:
| 'ConstVars' | Variables in wide to copy to tall without stacking. ConstVars is a positive integer, a vector of positive integers, a variable name, a cell array containing one or more variable names, or a logical vector. The default is all variables in wide not specified in datavar. |
| 'NewDataVarName' | A name for the data variable to be created in tall. The default is a concatenation of the names of the m variables that are stacked up. |
| 'DataIndVarName' | A name for the grouping variable to create in tall to indicate the source of each value in the new data variable. The default is based on the 'NewDataVarNames' parameter. |
You can also specify multiple groups of data variables in wide, each of which becomes a variable in tall. All groups must contain the same number of variables. Use a cell array to contain multiple parameter values for datavar, and a cell array of strings to contain multiple 'NewDataVarNames'.
Convert a wide format data set to tall format, and then back to a different wide format:
load flu flu2 = stack(flu, 2:11, 'NewDataVarName','FluRate',... 'IndVarName','Region') dateNames = cellstr(datestr(flu.Date,'mmm_DD_YYYY')); flu3 = unstack(flu2, 'FluRate', 'Date',... 'NewDataVarNames',dateNames)
dataset.grpstats | dataset.join | dataset.unstack
![]() | squeeze (categorical) | State property (qrandstream) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |