:
Name: Colon
Uses:
• Vector creation
• Indexing
• For-loop iteration
Description: Use the colon operator to create regularly spaced vectors, index into
arrays, and define the bounds of a for loop.
Examples
Create a vector:
x = 1:10
Create a vector that increments by 3:
x = 1:3:19
Reshape a matrix into a column vector:
A(:)
Assign new elements without changing the shape of an array:
A = rand(3,4);
A(:) = 1:12;
Index a range of elements in a particular dimension:
A(2:5,3)
Index all elements in a particular dimension:
A(:,3)
for loop bounds:
x = 1;
for k = 1:25
x = x + x^2;
end
More Information
• colon
• “Creating, Concatenating, and Expanding Matrices”
MATLAB Operators and Special Characters
2-7
Name: Colon
Uses:
• Vector creation
• Indexing
• For-loop iteration
Description: Use the colon operator to create regularly spaced vectors, index into
arrays, and define the bounds of a for loop.
Examples
Create a vector:
x = 1:10
Create a vector that increments by 3:
x = 1:3:19
Reshape a matrix into a column vector:
A(:)
Assign new elements without changing the shape of an array:
A = rand(3,4);
A(:) = 1:12;
Index a range of elements in a particular dimension:
A(2:5,3)
Index all elements in a particular dimension:
A(:,3)
for loop bounds:
x = 1;
for k = 1:25
x = x + x^2;
end
More Information
• colon
• “Creating, Concatenating, and Expanding Matrices”
MATLAB Operators and Special Characters
2-7
