doc2sequence — Convert tokenized documents to word-vector or word-index sequences.

doc2sequence(emb, documents) returns a cell array whose i-th cell contains the embedding-vector sequence for the i-th tokenized document. doc2sequence(enc, documents) returns word-index sequences from a wordEncoding object.

Syntax

sequences = doc2sequence(emb, documents)
sequences = doc2sequence(enc, documents)
sequences = doc2sequence(emb, documents, Name, Value)
sequences = doc2sequence(enc, documents, Name, Value)

Inputs

NameTypeRequiredDefaultDescription
embOrEncAnyYeswordEmbedding or wordEncoding object.
documentsAnyYestokenizedDocument object.
NameValueAnyVariadicName-value options: UnknownWord, PaddingDirection, PaddingValue, Length.

Returns

NameTypeDescription
sequencesAnyCell array of document embedding-vector or word-index sequences.

Errors

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

How doc2sequence works

  • The first input must be a RunMat wordEmbedding or wordEncoding compatibility object.
  • documents must be a RunMat tokenizedDocument compatibility object.
  • For embedding input, each output cell contains a D-by-S numeric matrix, where D is the embedding dimension and S is the sequence length.
  • For encoding input, each output cell contains a 1-by-S numeric vector of 1-based word encoding indices.
  • Unknown words are discarded by default. Set UnknownWord to "nan" to include NaN vector columns for embedding inputs or NaN index elements for encoding inputs.
  • PaddingDirection accepts "left", "right", and "none". The default is left padding.
  • PaddingValue accepts a numeric scalar and defaults to 0. MATLAB warns against NaN padding for deep learning workflows, but RunMat accepts numeric NaN for compatibility.
  • Length accepts "longest", "shortest", or a positive integer. Positive integer lengths truncate sequences on the right.
  • When PaddingDirection is "none", output cells keep per-document sequence lengths, except that fixed or shortest Length values still truncate longer documents.
  • Very large padded dense outputs are rejected with a controlled error to avoid accidental memory explosions.

GPU memory and residency

doc2sequence performs host text/object sequence construction and has no provider kernel.

Examples

Convert Documents To Word Vectors

emb = fastTextWordEmbedding;
documents = tokenizedDocument(["alpha beta"; "beta"]);
sequences = doc2sequence(emb, documents)

Expected output:

`sequences` is a cell array of D-by-S matrices.

Convert Documents To Word Indices

documents = tokenizedDocument(["alpha beta"; "beta"]);
enc = wordEncoding(documents);
sequences = doc2sequence(enc, documents)

Expected output:

`sequences` is a cell array of 1-by-S index vectors.

Avoid Padding

sequences = doc2sequence(emb, documents, "PaddingDirection", "none")

Expected output:

Each cell keeps the sequence length of the corresponding document.

Control Padding And Unknown Words

sequences = doc2sequence(emb, documents, "UnknownWord", "nan", "PaddingDirection", "right", "PaddingValue", -1, "Length", 100)

Expected output:

Sequences are right-padded or right-truncated to length 100.

Using doc2sequence with coding agents

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

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

FAQ

Does RunMat support doc2sequence(enc, documents) for wordEncoding?

Yes. RunMat returns one 1-by-S numeric index vector per tokenized document.

Does doc2sequence return single precision like MATLAB examples?

RunMat numeric tensors currently use host f64 storage, so the compatibility output is numeric but not MATLAB's exact single-precision storage.

Does doc2sequence execute on the GPU?

No. It maps host text/object metadata to host cell and tensor outputs.

Open-source implementation

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