wordEncoding — Create a word encoding object that maps words to indices and back.

wordEncoding(documents) creates a RunMat wordEncoding compatibility object from tokenized documents. wordEncoding(words) creates one from a word vector.

Syntax

enc = wordEncoding(documents)
enc = wordEncoding(words)
enc = wordEncoding(documents, Name, Value)

Inputs

NameTypeRequiredDefaultDescription
documentsOrWordsAnyYestokenizedDocument object or word vector.
NameValueAnyVariadicName-value options: Order, MaxNumWords.

Returns

NameTypeDescription
encAnyWord encoding compatibility object.

Errors

IdentifierWhenMessage
RunMat:wordEncoding:InvalidInputInputs do not match a supported wordEncoding form.wordEncoding received invalid input

How wordEncoding works

  • documents must be a RunMat tokenizedDocument compatibility object.
  • words may be a string scalar, string array, character vector, character matrix, or cell array of scalar text. A character vector is treated as one word.
  • The returned object exposes NumWords and Vocabulary. Vocabulary is a 1-by-NumWords string array.
  • Order accepts "first-seen" and "frequency" for tokenizedDocument input. The default is first-seen order.
  • For frequency ordering, RunMat sorts by descending total count and uses first-seen order as a deterministic tie-breaker.
  • MaxNumWords accepts a positive integer or Inf for tokenizedDocument input. The option is applied after ordering.
  • RunMat rejects Order and MaxNumWords when the first input is a word vector, matching the documented MATLAB syntax.
  • First-class MATLAB object-array identity semantics are not required for this scalar model object and remain covered by the broader object infrastructure work.

GPU memory and residency

wordEncoding is host text/object metadata construction with no provider kernel.

Examples

Create Encoding From Documents

documents = tokenizedDocument(["alpha beta alpha"; "gamma beta"]);
enc = wordEncoding(documents)

Expected output:

`enc.Vocabulary` is `["alpha" "beta" "gamma"]` for first-seen order.

Sort By Frequency

enc = wordEncoding(documents, "Order", "frequency", "MaxNumWords", 2)

Expected output:

`enc.Vocabulary` contains the two most frequent words.

Using wordEncoding with coding agents

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

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

FAQ

Can the result be used with doc2sequence?

Yes. doc2sequence(enc, documents) returns cell arrays of 1-by-S word-index vectors.

Does wordEncoding execute on the GPU?

No. It constructs host text metadata and does not use provider kernels.

Open-source implementation

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