vec2word — Map embedding vectors to nearest vocabulary words.
vec2word(emb, M) returns the nearest vocabulary word for each row of M. [words, dist] = vec2word(emb, M, k) returns the k nearest words and distances.
Syntax
words = vec2word(emb, M)
[words, dist] = vec2word(emb, M, k, 'Distance', distance)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
emb | Any | Yes | — | wordEmbedding object. |
M | NumericArray | Yes | — | Embedding vectors, one vector per row. |
k | NumericScalar | No | 1 | Number of nearest words. |
NameValue | Any | Variadic | — | Name-value options: Distance ('cosine' or 'euclidean'). |
Returns
| Name | Type | Description |
|---|---|---|
words | Any | Closest vocabulary words. |
dist | NumericArray | Distances to input vectors. |
Returned values from vec2word depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:vec2word:InvalidInput | Inputs do not match a supported vec2word form. | vec2word received invalid input |
How vec2word works
embmust be a RunMatwordEmbeddingcompatibility object.Mmust be ann-by-emb.Dimensionnumeric matrix.kis an optional positive integer from1through the vocabulary size. The default is1.- The default distance metric is cosine distance.
vec2word(___, 'Distance', 'euclidean')uses Euclidean distance. wordsis ann-by-kstring array.distis ann-by-knumeric matrix.- For zero-norm query or vocabulary vectors under cosine distance, RunMat reports infinite distance so finite matches sort ahead of them.
GPU memory and residency
vec2word currently compares host text-model storage and has no provider kernel.
Examples
Find Nearest Words
emb = readWordEmbedding('vectors.vec');
M = word2vec(emb, ["king" "queen"]);
words = vec2word(emb, M)Expected output:
`words` contains the nearest vocabulary word for each vector row.Return Multiple Neighbors
[words, dist] = vec2word(emb, M, 3, "Distance", "cosine")Expected output:
`words` and `dist` have three columns.Using vec2word with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how vec2word changes the result.
Run a small vec2word example, explain the result, then change one input and compare the output.
FAQ
Which distances are supported?⌄
RunMat supports "cosine" and "euclidean" in this slice.
Does vec2word execute on the GPU?⌄
No. It compares the host-resident embedding model.
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 · 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 vec2word is executed, line by line, in Rust.
- View the source for vec2word 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.