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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
embOrEnc | Any | Yes | — | wordEmbedding or wordEncoding object. |
documents | Any | Yes | — | tokenizedDocument object. |
NameValue | Any | Variadic | — | Name-value options: UnknownWord, PaddingDirection, PaddingValue, Length. |
Returns
| Name | Type | Description |
|---|---|---|
sequences | Any | Cell array of document embedding-vector or word-index sequences. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:doc2sequence:InvalidInput | Inputs do not match a supported doc2sequence form. | doc2sequence received invalid input |
How doc2sequence works
- The first input must be a RunMat
wordEmbeddingorwordEncodingcompatibility object. documentsmust be a RunMattokenizedDocumentcompatibility 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
UnknownWordto"nan"to include NaN vector columns for embedding inputs or NaN index elements for encoding inputs. PaddingDirectionaccepts"left","right", and"none". The default is left padding.PaddingValueaccepts a numeric scalar and defaults to 0. MATLAB warns against NaN padding for deep learning workflows, but RunMat accepts numeric NaN for compatibility.Lengthaccepts"longest","shortest", or a positive integer. Positive integer lengths truncate sequences on the right.- When
PaddingDirectionis"none", output cells keep per-document sequence lengths, except that fixed or shortestLengthvalues 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.
Related Strings functions
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · 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
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how doc2sequence is executed, line by line, in Rust.
- View the source for doc2sequence 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.