Colon :

Purpose

Create vectors, matrix subscripting, and for iterations.

Description

The colon is one of the most useful operators in MATLAB. It can create vectors, subscript matrices, and specify for iterations.

The colon operator uses the following rules to create regularly spaced vectors:

-------------------------------------------------------------------
j:k    is the same as [j,j+1,...,k]                                   
j:k    is empty if j > k                                           
j:i:k  is the same as [j,j+i,j+2i, ...,k]                             
j:i:k  is empty if i > 0 and j > k or if i < 0 and j < k  
-------------------------------------------------------------------
Below are the definitions that govern the use of the colon to pick out selected rows, columns, and elements of vectors and matrices:

-------------------------------------------------------------------
A(:,j)    is the j-th column of A                                     
A(i,:)    is the i-th row of A                                        
A(:,:)    is the same as A                                            
A(j:k)    is A(j), A(j+1),...,A(k)                                    
A(:,j:k)  is A(:,j), A(:,j+1),...,A(:,k)                              
A(:)      is all the elements of A, regarded as a single column. On   
          the left side of an assignment statement, A(:) fills A,     
          preserving its shape from before.                           
-------------------------------------------------------------------

Examples

Using the colon with integers,

D = 1:4
results in

D =
    1    2    3    4
Using two colons to create a vector with arbitrary real increments between the elements,

E = 0:.1:.5
results in

E =
    0    0.1000    0.2000    0.3000    0.4000    0.5000

See Also

for, linspace, logspace, reshape

(c) Copyright 1994 by The MathWorks, Inc.