mvnrnd — Generate multivariate normal random samples.

mvnrnd(mu,Sigma) draws rows from one or more multivariate normal distributions. mu supplies mean vectors and Sigma supplies covariance matrices.

Syntax

r = mvnrnd(mu, Sigma)
r = mvnrnd(mu, Sigma, n)

Inputs

NameTypeRequiredDefaultDescription
muNumericArrayYesMean vector or matrix with one mean vector per row.
SigmaNumericArrayYesCovariance matrix or covariance matrix pages.
nNumericArrayNoNumber of random rows to draw when mu is a single mean vector.

Returns

NameTypeDescription
rNumericArrayRandom samples with observations in rows.

Errors

IdentifierWhenMessage
RunMat:mvnrnd:InvalidArgumentInputs are malformed, dimensions are incompatible, or covariance pages are not symmetric positive semidefinite.mvnrnd: invalid argument
RunMat:mvnrnd:InternalRunMat cannot allocate or construct the requested random samples.mvnrnd: internal operation failed

How mvnrnd works

  • mu can be a scalar, a vector, or an m-by-d matrix with one mean vector per row. A d-by-1 column vector is treated as d one-dimensional means.
  • Sigma can be a d-by-d covariance matrix, a 1-by-d diagonal-variance vector, a d-by-d-by-m array of covariance pages, or a 1-by-d-by-m array of diagonal-variance pages.
  • mvnrnd(mu,Sigma,n) draws n rows when mu is a single mean vector and Sigma has one covariance page; n must be a positive scalar integer.
  • Without n, RunMat draws one row for each row of mu, each page of Sigma, or their shared row/page count.
  • A single covariance matrix is reused for all rows of matrix mu; a single mean vector is reused for all covariance pages.
  • Sigma must be symmetric positive semidefinite. Positive-definite covariances use Cholesky; singular semidefinite covariances use a symmetric eigen factor.
  • All inputs must be real finite numeric values, and outputs are double precision with observations in rows.

Examples

Draw five bivariate samples

rng('default');
R = mvnrnd([1 2], [4 1; 1 9], 5)

Use one covariance page per mean row

mu = [0 0; 10 20];
Sigma = cat(3, eye(2), [4 0; 0 9]);
R = mvnrnd(mu, Sigma)

Use diagonal variances

R = mvnrnd([1 2], [4 9], 5)

Draw from a singular covariance

R = mvnrnd([0 0], [1 1; 1 1], 3)

Using mvnrnd with coding agents

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

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

FAQ

Does mvnrnd use the same RNG as randn and normrnd?

Yes. RunMat draws standard normal variates from the shared deterministic RNG state, so rng controls reproducibility.

Does RunMat require Sigma to be positive definite?

No. Positive semidefinite covariance matrices are accepted. Negative eigenvalues outside numerical tolerance raise an error.

Open-source implementation

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