Skip to main content
v2026.11,986 entries · CC-BY 4.0

Standard Deviation in MATLAB: std Function Guide

MATLAB std computes the sample standard deviation (N−1) by default. Syntax for rows, all elements, missing values and weights, plus the NumPy trap.

Written and maintained by CASRAI Editorial Board

Published

On this page

In short: std(A) returns the sample standard deviation of the data in A, dividing by N−1. Pass 1 as the second argument (std(A,1)) to divide by N instead. This guide is for researchers and analysts who need to compute, check and report a standard deviation in MATLAB and avoid the common dimension, missing-value and cross-software mistakes.

What std is used for in research

The standard deviation summarises how spread out values are around their mean. In research it is reported alongside means in descriptive tables, used to standardise variables, and used as the scale for error bars and effect sizes. MATLAB’s std function computes it for vectors, matrices and N-dimensional arrays. Per the MathWorks reference page (read 2026-11-08), the default definition is S = √( (1/(N−1)) Σ|Aᵢ − μ|² ), where μ = (1/N) Σ Aᵢ: the mean always uses N, and the default sum of squares is divided by N−1.

Install or access

std is part of base MATLAB, so no toolbox is needed. The official reference is the MathWorks documentation page for std (listed under Sources). GNU Octave, a free program with a largely compatible syntax, also provides std (see the alternatives table below).

Step-by-step: computing a standard deviation

1. Standard deviation of a vector or of matrix columns

Call std(A). With no dimension given, MathWorks documents that it operates along the first array dimension whose size is not 1. For a matrix, that means one value per column. The documentation example uses A = [4 -5 1; 2 3 5; -9 1 7], and std(A) returns 7.0000 4.1633 3.0551.

2. Choose N−1 or N (sample or population)

The second argument is the normalisation weight w. w = 0 (the default) divides by N−1; w = 1 divides by N, giving the square root of the second moment about the mean. Use the default when your data are a sample and you want to estimate the spread of the wider population; use w = 1 when your data are the whole population of interest. A worked check by hand, for the eight values x = [2 4 4 4 5 5 7 9]: the mean is 5 and the squared deviations sum to 32. std(x,1) is √(32/8) = 2, and std(x) is √(32/7) ≈ 2.1381.

3. Standard deviation along rows

To choose a dimension while keeping the default normalisation, pass 0 as the second argument: std(A,0,2). The documentation example A = [6 4 23 -3; 9 -10 4 11; 2 8 -5 1] gives 11.0303; 9.4692; 5.3229, one value per row. Do not write std(A,2) expecting dimension 2: the second argument is always the weight w.

4. Several dimensions, or every element

Pass a vector of dimensions, as in std(A,0,[1 2]), to reduce over several dimensions at once (for a 3-D array this returns one value per page). Pass "all", as in std(A,0,"all"), to use every element in A.

5. Ignore missing values

The default missingflag is "includemissing": if a slice contains a missing value, its output is NaN. Add "omitmissing", as in std(A,0,"omitmissing"), to ignore missing values; a slice that is entirely missing still returns NaN. For numeric and duration data the older flag names "includenan" and "omitnan" are also documented, and "includenat"/"omitnat" for datetime.

6. Weighted standard deviation

Give w as a vector of nonnegative weights whose length matches the operating dimension. With A = [1 5; 3 7; -9 2] and w = [1 1 0.5], [S,M] = std(A,w) returns S = 4.4900 1.8330 and the weighted mean M = -0.2000 5.2000.

Output and how to read it

  • S has the same units as the data, and is never negative.
  • The optional second output M is the mean (the weighted mean when you supply weights), so [S,M] = std(A) gives both for a descriptive table.
  • A scalar input returns 0, and a 0-by-0 input returns NaN.
  • Report which normalisation you used. The sample (N−1) version is the usual default for inferential work.

Troubleshooting

Symptom Cause per the documentation Fix
Output is NaN for a column or the whole result A missing value is in the slice and the default is "includemissing" Use std(A,0,"omitmissing")
Still NaN after omitting The slice is entirely missing Check that column or row of the data
Got one value per column when you wanted per row Default operates along the first non-singleton dimension Use std(A,0,2)
std(A,2) does not give row results The second argument is the weight w, not the dimension Write std(A,0,2)
Result is 0 Scalar input (or a single observation) Confirm you passed the full data array
Weighted call errors The length of w must match the operating dimension Resize the weight vector

Free alternatives, and a cross-tool trap

The same data give different numbers in different programs if the default normalisation differs. The NumPy documentation notes that other libraries often default to ddof=1, unlike NumPy’s default of ddof=0.

Tool Call Default divisor Switch to the other
MATLAB std(A) N−1 std(A,1) for N
GNU Octave (free) std(x) N−1 (w = 0) std(x,1) for N
NumPy (free) np.std(a) N (ddof=0) np.std(a, ddof=1) for N−1

For the array [[1,2],[3,4]], np.std(a) is √(5/4) ≈ 1.1180 while ddof=1 gives √(5/3) ≈ 1.2910 (hand-calculated). Octave’s std also accepts w, a dimension, "all" and a nanflag ("omitnan" excludes NaNs).

Related CASRAI guides: MATLAB normal distribution, fitting in MATLAB, polyval in MATLAB, NumPy polyfit.

FAQ

Does MATLAB std use N or N−1?

N−1 by default (w = 0). Use std(A,1) to divide by N.

How do I get the standard deviation of each row?

Use std(A,0,2). The 0 keeps the default normalisation and 2 selects the row dimension.

How do I get one standard deviation for a whole matrix?

Use std(A,0,"all").

Why does std return NaN?

By default any missing value in a slice makes that output NaN. Use the "omitmissing" flag.

Why is my MATLAB result different from NumPy?

NumPy divides by N by default, MATLAB by N−1. Set ddof=1 in NumPy to match.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Standard Deviation in MATLAB: std Function Guide

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →