tsne — Embed high-dimensional observations using t-distributed stochastic neighbor embedding.

tsne(X) embeds the rows of a numeric observation matrix into a lower-dimensional numeric matrix. [Y,loss] = tsne(...) also returns the final Kullback-Leibler divergence.

Syntax

Y = tsne(X)
Y = tsne(X, Name, Value)
[Y, loss] = tsne(___)

Inputs

NameTypeRequiredDefaultDescription
XNumericArrayYesObservation matrix with observations in rows.
optionsAnyVariadicName-value options such as Algorithm, Distance, NumDimensions, NumPCAComponents, Perplexity, Standardize, InitialY, LearnRate, Options, Theta, and Verbose.

Returns

NameTypeDescription
YNumericArrayLow-dimensional embedding matrix.
lossNumericArrayKullback-Leibler divergence for the final embedding.

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

Errors

IdentifierWhenMessage
RunMat:tsne:InvalidArgumentInputs, dimensions, distances, initialization, or name-value options are malformed or unsupported.tsne: invalid argument
RunMat:tsne:InternalRunMat cannot allocate or construct the t-SNE result.tsne: internal error

How tsne works

  • X must be a real numeric vector or 2-D matrix. Matrix rows are observations; a column vector has one predictor per row, while a row vector is one observation with multiple predictors.
  • Rows containing NaN are omitted before fitting. Inf values are rejected.
  • NumDimensions selects the output dimension and defaults to 2. InitialY can match either all input rows or the complete rows that remain after NaN omission.
  • Algorithm accepts "barneshut" and "exact". RunMat computes a bounded exact CPU embedding for both modes so scripts receive compatible outputs without silent shape changes.
  • Distance supports "euclidean", "fasteuclidean", "seuclidean", "fastseuclidean", "cityblock", "chebychev", "minkowski", "mahalanobis", "cosine", "correlation", "spearman", "hamming", and "jaccard". The "minkowski" option uses the default exponent 2. Custom function-handle distances are rejected explicitly.
  • Standardize centers and scales each predictor column before distance computation. NumPCAComponents applies PCA before pairwise distance computation when it is positive and smaller than the predictor count.
  • Perplexity, Exaggeration, LearnRate, Theta, Verbose, NumPrint, and CacheSize are parsed and validated for MATLAB-style script compatibility. Theta, Verbose, NumPrint, and CacheSize do not affect the exact CPU optimizer.
  • Options accepts a statset-style struct with MaxIter, TolFun, and empty OutputFcn. Nonempty OutputFcn is rejected because RunMat does not invoke iteration callbacks from builtins yet.
  • RunMat rejects very large dense exact workloads before allocation to avoid unbounded pairwise, PCA, and Mahalanobis memory use.

Examples

Embed observations into two dimensions

X = [0 0; 0.2 0.1; 9.8 9.9; 10 10.1];
Y = tsne(X, 'Perplexity', 2)

Expected output:

`Y` is a 4-by-2 embedding matrix.

Request the final loss

[Y,loss] = tsne(X, 'Algorithm', 'exact', 'Options', struct('MaxIter', 250))

Expected output:

`loss` is the final Kullback-Leibler divergence for the embedding.

Omit incomplete rows

Y = tsne([1 2; NaN 3; 10 11])

Expected output:

`Y` has two rows because the incomplete observation is omitted.

Using tsne with coding agents

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

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

FAQ

Does RunMat implement Barnes-Hut acceleration?

No. Algorithm="barneshut" is accepted for compatibility, but RunMat currently uses its bounded exact CPU optimizer for both algorithm names.

Are stochastic results bit-for-bit identical to MATLAB?

No. RunMat uses its own RNG stream behind rng, so output shape and semantics are compatible but random initial embeddings are not bit-for-bit identical.

Open-source implementation

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