onehotdecode — Decode one-hot vectors into class labels.

onehotdecode(B,classes,featureDim) chooses the maximum value along featureDim and maps the index to classes.

Syntax

A = onehotdecode(B, classes, featureDim)
A = onehotdecode(B, classes, featureDim, typename)
A = onehotdecode(B, classes, featureDim, Name, Value)

Inputs

NameTypeRequiredDefaultDescription
BAnyYesOne-hot encoded data.
classesAnyYesClass names corresponding to the one-hot feature dimension.
featureDimIntegerScalarYesDimension containing or receiving one-hot feature vectors.
typenameStringScalarNoRequested output type.
optionsAnyVariadicName-value options such as ClassNames or OutputType.

Returns

NameTypeDescription
AAnyDecoded labels.

Errors

IdentifierWhenMessage
RunMat:onehotdecode:InvalidArgumentInputs, dimensions, classes, output types, or options are malformed.encoding: invalid argument
RunMat:onehotdecode:InternalInternal conversion or allocation fails.encoding: internal error

How onehotdecode works

  • The length of classes must match the size of B along featureDim.
  • B must be numeric or logical with non-NaN values in the range [0,1]; NaN entries are ignored while choosing the maximum class.
  • Ties are resolved by the first class along featureDim, matching MATLAB-style first-maximum behavior.
  • The decoded output keeps the encoded shape with the feature dimension set to 1.
  • By default the decoded output is categorical and preserves the supplied class order as the category order.
  • An optional typename can request double, logical, string, cellstr, or categorical output; OutputType is also accepted as a RunMat extension.
  • Numeric output requires numeric classes, and logical output requires logical classes.

Example

Decode logical one-hot rows

A = onehotdecode(logical([0 1; 1 0]), ["blue" "red"], 2, 'string')

Expected output:

A is a 2-by-1 string array.

Using onehotdecode with coding agents

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

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

FAQ

What happens when a vector contains all zeros?

The first class is selected because all classes tie at zero.

Open-source implementation

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