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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
mu | NumericArray | Yes | — | Mean vector or matrix with one mean vector per row. |
Sigma | NumericArray | Yes | — | Covariance matrix or covariance matrix pages. |
n | NumericArray | No | — | Number of random rows to draw when mu is a single mean vector. |
Returns
| Name | Type | Description |
|---|---|---|
r | NumericArray | Random samples with observations in rows. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:mvnrnd:InvalidArgument | Inputs are malformed, dimensions are incompatible, or covariance pages are not symmetric positive semidefinite. | mvnrnd: invalid argument |
RunMat:mvnrnd:Internal | RunMat cannot allocate or construct the requested random samples. | mvnrnd: internal operation failed |
How mvnrnd works
mucan 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.Sigmacan 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)drawsnrows whenmuis a single mean vector andSigmahas one covariance page;nmust be a positive scalar integer.- Without
n, RunMat draws one row for each row ofmu, each page ofSigma, 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. Sigmamust 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.
Related Stats functions
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mvnrnd is executed, line by line, in Rust.
- View the source for mvnrnd 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.