Main Content

Compute by Group

Summarize, transform, or filter by group

Description

The Compute by Group task lets you interactively group data and compute summary statistics, transformations, or filtering operations on each group. The task automatically generates MATLAB® code for your live script.

Using this task, you can:

  • Define groups of data in an array, table, or timetable.

  • Compute statistics, make a transformation, or filter the data based on each grouping.

  • Output a new table or timetable with the results of the computation.

Compute by Group task in Live Editor

Open the Task

To add the Compute by Group task to a live script in the MATLAB Editor:

  • On the Live Editor tab, select Task > Compute by Group.

  • In a code block in the script, type a relevant keyword, such as group. Select Compute by Group from the suggested command completions.

Parameters

Specify groups by selecting valid workspace grouping variables from the Group by drop-down list. When the data is contained in a table or timetable, additionally select the table variables to group by. You can group by unique values or specify how to bin the data.

From the Compute on drop-down list, select the workspace data to compute on. When the data is contained in a table or timetable, select which table variables to compute on.

Select one of the following computation options.

Computation TypeDescription
Compute stats by groupA summary (or aggregate) of data, such as a mean or maximum. You can also supply a custom function by providing a local function name or a function handle. The function must return one entity per group whose first dimension has length 1. For more information, see groupsummary.
Transform by groupTransform the data, for example, scale the data by the 2-norm or fill missing data. You can also supply a custom function by providing a local function name or a function handle. The function must return one entity whose first dimension has length 1 or has the same number of rows as the input data. For more information, see grouptransform.
Filter by groupFilter members from each group by providing a local function or function handle that defines the filtering computation. The function must return a logical scalar or a logical column vector with the same number of rows as the data indicating which group members to select. If the function returns a logical scalar, then either all members of the group are filtered (when the value is false) or none are (when the value is true). If the function returns a logical vector, then members of groups are filtered when the corresponding element is false. Members are kept when the corresponding element is true. For more information, see groupfilter.

For all computation types, you can click New to create a new function in the Live Script that defines the computation. Clicking New automatically inserts an example function into the Live Script that uses the appropriate syntax for the selected computation type. If you change the name of the example function, to use the new function name, reselect the method from the drop-down list in the live task.

Introduced in R2021b