Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

Double Colon Operator in MATLAB: start:step:stop

The MATLAB double colon (start:step:stop) builds vectors, slices arrays and drives loops. Includes empty-result rules and the floating-point endpoint trap.

Written and maintained by CASRAI Editorial Board

Last updated

The “double colon” in MATLAB is the two-colon form start:step:stop, which builds an evenly spaced row vector. MATLAB has no :: operator; the colon operator takes a start, an optional step and a stop, and the default step is 1. This page is for researchers and analysts who need to generate sequences, slice arrays and drive loops with it, and who need to know why 0:0.3:1 does not end at 1.

What it is used for in research

MathWorks documents the colon as an operator that can create vectors, subscript arrays and specify iterations. In analysis scripts that means three recurring jobs: building a time or parameter axis (t = 0:0.1:10), selecting rows or columns of a data matrix (A(:,2:3)), and looping over indices (for n = 1:4). The same character does all three, which is why the forms are easy to confuse.

Form Meaning Role
j:k Vector from j to k in steps of 1 Vector creation
j:i:k Vector from j to k in steps of i (the “double colon” form) Vector creation
A(:,n), A(m,:) Column n, row m of A Indexing
A(j:k) Elements j through k of A Indexing
A(:) All elements of A as one column vector Indexing / reshape
for n = j:k Iterate n over the vector Loop iteration

Install or access (official source only)

The colon operator is built into the MATLAB language, so no toolbox or add-on is needed. Obtain MATLAB from MathWorks (mathworks.com/products/matlab.html) and use the official reference page for the operator, Colon, for the exact current syntax. Many institutions provide campus licences; check with your research computing or IT office.

Step-by-step: build and use a start:step:stop vector

  1. Create a vector with the default step of 1: x = 1:10 returns the integers 1 through 10 (a 1-by-10 row vector).
  2. Add a step as the middle value: x = 0:2:10 returns 0 2 4 6 8 10 (1-by-6).
  3. Count down with a negative step: x = 6:-1:0 returns 6 5 4 3 2 1 0 (1-by-7). MathWorks states that to decrement you use a negative number.
  4. Use a fractional step: x = 1:0.2:2.1 returns 1.0 1.2 1.4 1.6 1.8 2.0 (1-by-6). The sequence ends at the last value it can reach before exceeding the range, so 2.1 is not included.
  5. Select from a matrix: for a 3-by-3 matrix A, A(1,:) is the first row, A(:,2:3) is columns 2 and 3, and A(:) is a 9-by-1 column.
  6. Loop over the vector: for n = 1:4 runs the loop body four times with n = 1, 2, 3, 4. See loops in MATLAB for the full loop syntax.

Output and how to read it

The result of j:i:k is a row vector, so the size is always 1-by-N. Read the count from the size, not from the arithmetic you expect: -2.5:2.5 gives -2.5 -1.5 -0.5 0.5 1.5 2.5, which is 1-by-6. To get a column instead, transpose the result or index with A(:) on an existing array. For how indexing works once you have the vector, see indexing in MATLAB, and for building and combining arrays see matrices in MATLAB.

Empty and floating-point results

When the result is empty

Per the MathWorks reference, j:k is empty when j is greater than k. j:i:k is empty when any input is empty, when i is 0, when i is positive and j is greater than k, or when i is negative and j is less than k. A positive step with a start above the stop (10:2:0) therefore returns an empty vector rather than an error; use 10:-2:0 to count down.

The non-integer step trap

With a non-integer increment, floating-point arithmetic decides whether the endpoint is included. MathWorks notes that 0:0.3:1 does not end at 1 (the last element is 0.9). If you need a fixed number of points that includes both endpoints, MathWorks points to linspace instead; see linspace in MATLAB.

Troubleshooting

Symptom Cause (per the MathWorks reference)
Result is 1x0 empty Step sign does not match the direction from start to stop, or step is 0
Last expected point is missing Stop is not reachable in whole steps, or floating-point arithmetic excluded it; use linspace for guaranteed endpoints
Got a column when a row was expected A(:) always returns a column; colon-built vectors are rows

Free alternatives

If you do not have a MATLAB licence, these tools express the same idea. Syntax differs, so check each project’s own documentation.

Tool Equivalent Note
GNU Octave 1:3:5 The Octave manual (v9.2.0, “Ranges”) defines a range by base, optional increment and a limit the elements do not exceed, with default increment 1; 1:3:5 gives [1, 4]
Python / NumPy numpy.arange(start, stop, step) Stop is exclusive, unlike MATLAB
R seq(from, to, by) Base R function
Julia start:step:stop Same order of arguments as MATLAB

FAQ

Is there a :: operator in MATLAB?

No. “Double colon” refers to the two colons in start:step:stop. A single colon (j:k) uses the default step of 1.

What does A(:) do?

It reshapes all elements of A into a single column vector, and has no effect if A is already a column.

How do I count down?

Use a negative step, for example 10:-2:0.

Sources (official docs)

Follow CASRAI

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

Ask CASRAI · free to try

Ask about Double Colon Operator in MATLAB: start:step:stop

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 →