wordEncoding — Create a word encoding object that maps words to indices and back.
wordEncoding(documents) creates a wordEncoding 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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
documentsOrWords | Any | Yes | — | tokenizedDocument object or word vector. |
NameValue | Any | Variadic | — | Name-value options: Order, MaxNumWords. |
Returns
| Name | Type | Description |
|---|---|---|
enc | Any | Word encoding compatibility object. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:wordEncoding:InvalidInput | Inputs do not match a supported wordEncoding form. | wordEncoding received invalid input |
How wordEncoding works
documentsmust be atokenizedDocumentobject.wordsmay 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
NumWordsandVocabulary.Vocabularyis a 1-by-NumWords string array. Orderaccepts"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.
MaxNumWordsaccepts a positive integer-valued double orInffor tokenizedDocument input. The option is applied after ordering.- A native integer scalar for
MaxNumWordsis a RunMat language extension. It is accepted inrunmatmode and decoded exactly as a positive structural count. - RunMat rejects
OrderandMaxNumWordswhen 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 constructs host text and object metadata and does not launch provider kernels.
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.
Related Strings functions
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · trainWordEmbedding · vaderSentimentScores · vec2word · word2ind · word2vec · writeWordEmbedding
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp · strncmpi · strtok · unicode2native
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
Related guides
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how wordEncoding is executed, line by line, in Rust.
- View the source for wordEncoding in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.