| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
The colon is one of the most useful operators in MATLAB. It can create vectors, subscript arrays, and specify for iterations.
The colon operator uses the following rules to create regularly spaced vectors:
is the same as [j,j+1,...,k] | |
is empty if j > k | |
is the same as [j,j+i,j+2i, ...,k] | |
is empty if i == 0, if i > 0 and j > k, or if i < 0 and j < k |
where i, j, and k are all scalars.
Below are the definitions that govern the use of the colon to pick out selected rows, columns, and elements of vectors, matrices, and higher-dimensional arrays:
For more information on how the colon operator works, see http://www.mathworks.com/support/solutions/data/1-4FLI96.html?solution=1-4FLI96.
Using the colon with integers,
D = 1:4
results in
D =
1 2 3 4Using 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.5000The command
A(:,:,2) = pascal(3)
generates a three-dimensional array whose first page is all zeros.
A(:,:,1) =
0 0 0
0 0 0
0 0 0
A(:,:,2) =
1 1 1
1 2 3
1 3 6Using a colon with characters to iterate a for-loop,
for x='a':'d',x,end
results in
x =
a
x =
b
x =
c
x =
d
for, linspace, logspace, reshape
![]() | Special Characters [ ] ( ) {} = ' . ... , ; : % ! @ | abs | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |