Compute by Group
Summarize, transform, or filter by group
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.
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.
Input data — Valid grouping data from workspace
vector | matrix | table | timetable | ...
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.
Group computation — Type of computation to perform
Compute stats by group |
Transform by group |
Filter by group | ...
Select one of the following computation options.
|A 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 |
|Transform 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 |
|Filter 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 |
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.