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

NameTypeRequiredDefaultDescription
embAnyYeswordEmbedding object.
MNumericArrayYesEmbedding vectors, one vector per row.
kNumericScalarNo1Number of nearest words.
NameValueAnyVariadicName-value options: Distance ('cosine' or 'euclidean').

Returns

NameTypeDescription
wordsAnyClosest vocabulary words.
distNumericArrayDistances to input vectors.

Returned values from vec2word depend on how many outputs the caller requests.

Errors

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

How vec2word works

  • emb must be a RunMat wordEmbedding compatibility object.
  • M must be an n-by-emb.Dimension numeric matrix.
  • k is an optional positive integer from 1 through the vocabulary size. The default is 1.
  • The default distance metric is cosine distance. vec2word(___, 'Distance', 'euclidean') uses Euclidean distance.
  • words is an n-by-k string array. dist is an n-by-k numeric 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.

Open-source implementation

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