grpstats — Compute summary statistics organized by group.

grpstats(X,group) computes grouped summary statistics for numeric or logical matrices. grpstats(T,groupvars,...) returns a table containing grouping variables, group counts, and requested summary columns.

Syntax

tblstats = grpstats(tbl, groupvars)
stats = grpstats(X, group)
[stats1, ..., statsN] = grpstats(X, group, whichstats)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesInput matrix or table.
groupAnyYesGrouping variables, variable selectors, or empty grouping.
whichstatsAnyNo"mean"Summary statistic name or names.
nameValuePairsAnyVariadicAlpha, DataVars, and VarNames options.

Returns

NameTypeDescription
outAnyResult value.

Returned values from grpstats depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:table:InvalidArgumentArguments or table metadata are invalid.table: invalid argument
RunMat:table:InvalidIndexTable indexing is invalid.table: invalid index
RunMat:table:InvalidVariableA table variable name or value is invalid.table: invalid variable

How grpstats works

  • For matrix input, group can be numeric, logical, string, char, categorical, or cell text data. A cell array of grouping variables such as {G1,G2} creates groups from observed value combinations. Empty grouping such as [] computes one all-row group.
  • For matrix input, whichstats accepts "mean", "sem", "std", "var", "min", "max", "range", "meanci", "predci", "numel", and "gname". Multiple requested statistics return one output per statistic when multiple outputs are requested.
  • meanci and predci return ngroups x ncols x 2 tensors and honor the "Alpha" option. Intervals use Student's t critical values.
  • Rows with missing group labels are ignored. NaN values in data variables are omitted from numeric summaries.
  • For table input, groupvars accepts table variable selectors or [] for no grouping. Output tables include group variables, GroupCount, and one variable for each data variable/statistic combination. Interval statistics are stored as two-column table variables.
  • For table input, "DataVars" selects numeric or logical data variables and "VarNames" replaces the output variable names. Built-in function handles such as @mean and @std are accepted when they map to supported statistic names; custom function handles are rejected explicitly because general MATLAB function-handle dispatch is not yet implemented for grpstats.

Examples

Compute group means for a matrix

X = [1 10; 3 30; 2 20; 4 40];
g = [2; 1; 2; 1];
m = grpstats(X,g)

Expected output:

m is [3.5 35; 1.5 15] for groups 1 and 2.

Return multiple matrix statistics and group names

[m,s,names] = grpstats(X,g,["mean","std","gname"])

Expected output:

`m` and `s` are numeric arrays; `names` is a cell array of group labels.

Summarize a table with selected data variables

T = table(G,X,Y);
S = grpstats(T,"G",["mean","max"],"DataVars","X")

Expected output:

`S` contains `G`, `GroupCount`, `mean_X`, and `max_X`.

Using grpstats with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how grpstats changes the result.

Run a small grpstats example, explain the result, then change one input and compare the output.

FAQ

Does grpstats support function handles?

RunMat accepts built-in function handles whose names match supported statistics, such as @mean and @std. Custom function handles are not yet supported.

How are groups ordered?

Numeric and logical groups sort by value. Text groups preserve first-seen order, matching MATLAB's character and string grouping behavior.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how grpstats is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.