Compute by Group
Summarize, transform, or filter by group in the Live Editor
Description
The Compute by Group task lets you interactively group data and compute summary statistics, perform transformations, or apply filters for 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.
Summarize, transform, 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 Live Editor:
On the Live Editor tab, click Task and select the Compute by Group icon
.
In a code block in the live script, type a relevant keyword, such as
group
. Select Compute by Group from the suggested command completions.
Examples
Related Examples
Parameters
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 the 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 these computation options:
Computation Type | Description |
---|---|
Compute stats by group | 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 groupsummary . |
Transform by group | 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 grouptransform . |
Filter by group | 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 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.