Create Artificial Data in MATLAB
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
MATLAB provides a rich set of functions for generating artificial (synthetic) data for testing algorithms, simulating systems, and prototyping models before real data is available. The core functions are rand (uniform), randn (normal/Gaussian), and randi (random integers) for basic random data. For structured synthetic data, MATLAB offers functions to create linear trends, sinusoidal signals, noisy datasets, clustered data, and time series. This article covers the most common data generation patterns used in engineering, machine learning, and statistical analysis.
Random Number Generation
Uniform Distribution (rand)
Normal (Gaussian) Distribution (randn)
Random Integers (randi)
Structured Data Generation
Linear Data with Noise
Sinusoidal Signal with Noise
Polynomial Data
Classification Data (Clusters)
Two-Class Gaussian Clusters
Multi-Class Clusters
Time Series Data
Autoregressive (AR) Process
Specific Distributions
Common Pitfalls
- Not setting the random seed for reproducibility: Without
rng(seed), every run generates different data, making debugging impossible. Always set a seed at the start of your script when you need reproducible results. - Confusing
randandrandn:randgenerates uniform numbers in [0, 1].randngenerates standard normal numbers (mean 0, std 1). Usingrandwhen you need Gaussian data (or vice versa) produces incorrect distributions. - Wrong matrix dimensions with
rand(n):rand(n)creates an n-by-n matrix, not an n-by-1 vector. For a column vector, userand(n, 1). For a row vector, userand(1, n). - Generating correlated features unintentionally:
[rand(n,1), rand(n,1)]creates independent features. If you need correlated features, usemvnrndwith a specified covariance matrix, or apply a transformation like Cholesky decomposition. - Using integer seeds with
rnginconsistently across MATLAB versions:rng(42)uses the Mersenne Twister by default, but older MATLAB versions may use a different generator. Specify the generator explicitly withrng(42, 'twister')for cross-version reproducibility.
Summary
- Use
rand,randn, andrandifor uniform, Gaussian, and integer random data - Scale and shift with
a + (b-a)*rand(n,1)for custom ranges ormu + sigma*randn(n,1)for custom normal distributions - Generate structured data by combining linear/polynomial/sinusoidal functions with noise
- Use
mvnrndfor multivariate normal data with specified correlations - Always set
rng(seed)for reproducible synthetic data generation
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.