RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • 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
    • wordEncoding
    • writeWordEmbedding

bagOfWords — Create bag-of-words model objects.

bagOfWords(documents) creates a lightweight RunMat bagOfWords object from a tokenizedDocument compatibility object. bagOfWords(uniqueWords, counts) creates a model from an explicit vocabulary and count matrix.

Syntax

bag = bagOfWords
bag = bagOfWords(documents)
bag = bagOfWords(uniqueWords, counts)

Inputs

NameTypeRequiredDefaultDescription
documentsOrWordsAnyYes—Tokenized documents, word vector, or unique vocabulary.
uniqueWordsAnyYes—Unique words.
countsAnyYes—Word counts per document.

Returns

NameTypeDescription
bagAnyBag-of-words model object.

Errors

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

How bagOfWords works

  • bagOfWords with no input creates an empty model.
  • bagOfWords(documents) accepts RunMat tokenizedDocument objects created by tokenizedDocument.
  • bagOfWords(words) accepts a word vector as a single-document model.
  • bagOfWords(uniqueWords, counts) accepts a word vector and a nonnegative integer-valued count matrix whose columns match the vocabulary length. All eight integer classes are supported; values remain authoritative through exact validation and vocabulary-column filtering before one deliberate conversion into the double Counts property.
  • uniqueWords must not contain duplicate nonmissing words. Missing string entries are ignored along with their corresponding count columns.
  • The returned object stores Vocabulary, Counts, NumWords, and NumDocuments properties.
  • Counts are stored as a dense host double tensor with one row per document and one column per vocabulary word. Very large dense count matrices are rejected with a controlled error until native sparse bag storage is implemented.
  • GPU-resident inputs reject before provider access because bagOfWords exposes no interactive GPU-array input surface.
  • Native sparse bag storage and sparse-preserving GPU residency remain outside this slice.
  • Vocabulary order follows first token appearance for document input and the supplied order for explicit uniqueWords input.

GPU memory and residency

bagOfWords stores host text-model metadata and dense host double counts in this slice; resident inputs reject before provider access.

Examples

Create A Bag From Documents

documents = tokenizedDocument(["alpha beta"; "beta gamma"]);
bag = bagOfWords(documents)

Expected output:

`bag.NumDocuments` is `2`, and `bag.Counts` has one column for each unique token.

Create A Bag From Counts

bag = bagOfWords(["alpha" "beta"], [2 1; 0 3])

Expected output:

`bag.Vocabulary` is `["alpha" "beta"]`, and `bag.Counts` is the supplied `2 x 2` count matrix.

Using bagOfWords with coding agents

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

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

FAQ

Does bagOfWords use sparse matrices?⌄

Not in this slice. The compatibility object stores dense host counts and rejects very large dense count matrices. Sparse storage and provider-resident sparse paths remain broader runtime/model work.

Can bagOfWords consume raw document strings directly?⌄

No. Raw document arrays should first be converted with tokenizedDocument. A raw word vector is accepted as a single-document model.

Does bagOfWords execute on the GPU?⌄

No. It builds a host text-model object and has no runmat-accelerate provider path. Resident inputs reject rather than gathering.

Related Strings functions

Text Analytics

addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · 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 · wordEncoding · 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

Regex

regexp · regexpi · regexprep

Open-source implementation

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

  • View the source for bagOfWords 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.

Getting started · Benchmarks · Pricing

Download RunMat

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

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How bagOfWords works
  • GPU memory and residency
  • Examples
  • Create A Bag From Documents
  • Create A Bag From Counts
  • Using bagOfWords with coding agents
  • FAQ
  • Related Strings functions
  • Text Analytics
  • Transform
  • Core
  • Search
  • Pattern
  • Regex
  • Open-source implementation
  • About RunMat