Documentation

colon,:

Vector creation, array subscripting, andfor-loop iteration

The colon is one of the most useful operators in MATLAB®. It can create vectors, subscript arrays, and specifyforiterations.

Syntax

x = j k x = j:我:k (:, n) (m,:) A(:) A(j:k)

Description

example

x=j:kcreates a unit-spaced vectorxwith elements[j,j+1,j+2,...,j+m]wherem = fix(k-j). Ifjandkare both integers, then this is simply[j,j+1,...,k].

example

x=j:i:kcreates a regularly-spaced vectorxusingias the increment between elements. The vector elements are roughly equal to[j,j+i,j+2*i,...,j+m*i]wherem = fix((k-j)/i). However, ifiis not an integer, then floating point arithmetic plays a role in determining whethercolonincludes the endpointkin the vector, sincekmight not beexactlyequal toj+m*i. If you specify nonscalar arrays, then MATLAB interpretsj:i:kasj(1):i(1):k(1).

x = colon(j,k)andx = colon(j,i,k)are alternate ways to execute the commandsj:kandj:i:k, but are rarely used. These syntaxes enable operator overloading for classes.

example

A(:,n),A(m,:),A(:), andA(j:k)are common indexing expressions for a matrixAthat contain a colon. When you use a colon as a subscript in an indexing expression, such asA(:,n), it acts as shorthand to includeallsubscripts in a particular array dimension. It is also common to create a vector with a colon for the purposes of indexing, such asA(j:k). Some indexing expressions combine both uses of the colon, as inA(:,j:k).

Common indexing expressions that contain a colon are:

  • A(:,n)is thenth column of matrixA.

  • A(m,:)is themth row of matrixA.

  • A(:,:,p)is thepth page of three-dimensional arrayA.

  • A(:)reshapes all elements ofAinto a single column vector. This has no effect ifAis already a column vector.

  • A(:,:)reshapes all elements ofAinto a two-dimensional matrix. This has no effect ifAis already a matrix or vector.

  • A(j:k)uses the vectorj:kto index intoAand is therefore equivalent to the vector[A(j), A(j+1), ..., A(k)].

  • A(:,j:k)includes all subscripts in the first dimension but uses the vectorj:kto index in the second dimension. This returns a matrix with columns[A(:,j), A(:,j+1), ..., A(:,k)].

Examples

全部折叠

Create a unit-spaced vector of numbers between 1 and 10. The colon operator uses a default increment of +1.

x = 1:10
x =1 2 3 4 5 6 7 8 9 10

Create vectors that increment or decrement by a specified value.

Create a vector whose elements increment by 0.1.

x = 0:0.1:1
x =Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 Columns 8 through 11 0.7000 0.8000 0.9000 1.0000

Create a vector whose elements decrement by -2.

y = 10:-2:0
y =10 8 6 4 2 0

Examine several ways to index a matrix using a colon:.

Create a 3-by-3 matrix. Index the first row.

A = magic(3)
A =8 1 6 3 5 7 4 9 2
A(1,:)
ans =8 1 6

Index the second and third column.

A(:,2:3)
ans =1 6 5 7 9 2

Reshape the matrix into a column vector.

A(:)
ans =8 3 4 1 5 9 6 7 2

In the context of afor-loop, the colon specifies the loop iterations.

Write afor-loop that squares a number for values ofnbetween 1 and 4.

forn = 1:4 n^2end
ans = 1
ans = 4
ans = 9
ans = 16

Input Arguments

全部折叠

Starting vector value, specified as a real numeric scalar. Ifj < kso that the output vector is not empty, thenjis the first element in the vector.

Example:x = 0:5

Example:x = 0:0.5:5

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|char|datetime|duration

Ending vector value, specified as a real numeric scalar.kis the last value in the vector only when the increment lines up to exactly land onk. For example, the vector0:5includes 5 as the last value, but0:0.3:1does not include the value 1 as the last value since the increment does not line up with the endpoint.

Example:x = 0:5

Example:x = 0:0.5:5

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|char|datetime|duration

Increment between vector elements, specified as a real numeric scalar.

Example:x = 0:0.5:5

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|char|datetime|duration

Output Arguments

全部折叠

Regularly-spaced vector, returned as a row vector. Ifj > k, thenx = j:kis an empty matrix. More generally, the syntaxx = j:i:kreturns an empty matrix when:

  • i,j, orkis an empty input

  • i == 0

  • i > 0andj > k

  • i < 0andj < k

Tips

  • Theforreference page has a description of how to use:in the context of loop statements.

  • linspaceis similar to the colon operator:, but it gives direct control over the number of points and always includes the endpoints. The sibling functionlogspacegenerates logarithmically spaced values.

  • When you create a vector to index into a cell array or structure array (such ascellName{:}orstructName(:).fieldName), MATLAB returns multiple outputs in a comma-separated list. For more information, seeHow to Use the Comma-Separated Lists.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?