confusionmat — Compute a confusion matrix from true and predicted class labels.

confusionmat(group,grouphat) compares true class labels in group with predicted labels in grouphat and returns a matrix whose rows are true classes and columns are predicted classes.

Syntax

C = confusionmat(group, grouphat)
C = confusionmat(group, grouphat, Name, Value)
[C,order] = confusionmat(___)

Inputs

NameTypeRequiredDefaultDescription
groupAnyYesTrue class labels.
grouphatAnyYesPredicted class labels.
nameValuePairsAnyVariadicName-value options such as Order.

Returns

NameTypeDescription
CNumericArrayConfusion matrix with true labels in rows and predicted labels in columns.
orderAnyClass labels corresponding to rows and columns of C.

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

Errors

IdentifierWhenMessage
RunMat:confusionmat:InvalidArgumentLabel vectors, dimensions, order labels, or name-value options are malformed.confusionmat: invalid argument
RunMat:confusionmat:InternalRunMat cannot allocate or construct confusion matrix outputs.confusionmat: internal error

How confusionmat works

  • group and grouphat must contain the same number of labels.
  • Numeric, logical, string, character-row, categorical, and cell-string labels are supported. Text label families can be mixed between string, char, cell-string, and categorical inputs.
  • Without an explicit order, numeric labels are sorted ascending, text labels follow first appearance across group then grouphat, categorical labels follow category order for observed categories, and logical labels are ordered false then true.
  • confusionmat(group,grouphat,'Order',order) uses the supplied class order, requires every nonmissing label in group and grouphat to appear in order, and includes zero rows or columns for extra order labels that do not appear in the data.
  • Rows with missing true or predicted labels are omitted. Numeric NaN, empty text, and <missing> text are treated as missing labels.
  • [C,order] = confusionmat(...) returns the class labels corresponding to the rows and columns of C, preserving the label family of the inferred or explicit order.

Examples

Build a numeric confusion matrix

truth = [1; 1; 2; 2];
pred = [1; 2; 2; 1];
[C,order] = confusionmat(truth, pred)

Expected output:

C is [1 1; 1 1] and order is [1; 2].

Use an explicit class order

C = confusionmat(["cat";"dog";"dog"], ["dog";"dog";"cat"], 'Order', ["dog";"cat"])

Expected output:

Rows and columns are ordered as dog, then cat.

Using confusionmat with coding agents

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

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

FAQ

Does confusionmat normalize counts?

No. It returns raw counts. Normalize rows or columns explicitly after computing C.

How are labels not present in an explicit Order handled?

When Order is supplied, it must contain every nonmissing true and predicted label. Extra order labels are allowed and produce zero rows or columns.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how confusionmat 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.