MATLAB - Colon Notation
MATLAB - The Colon (:) Operator and Indexing
Section titled “MATLAB - The Colon (:) Operator and Indexing”The colon : is one of the most powerful and fundamental operators in MATLAB. It is used for creating vectors, iterating in loops, and, most importantly, for indexing and selecting subsets of data from matrices and arrays. Mastering its use is key to writing efficient, concise, and idiomatic MATLAB code.
Creating Vectors
Section titled “Creating Vectors”The colon operator provides a compact syntax for creating sequences of numbers.
start:end: Creates a vector fromstarttoendwith a default increment of 1.start:step:end: Creates a vector fromstarttoendwith a specified increment ofstep.
%% Create a vector from 1 to 10 (increment of 1)v1 = 1:10% ans = 1 2 3 4 5 6 7 8 9 10
%% Create a vector from 100 down to 50 in steps of -5v2 = 100:-5:50% ans = 100 95 90 85 80 75 70 65 60 55 50
%% Create a vector of radian values from 0 to piv3 = 0:pi/4:pi% ans = 0 0.7854 1.5708 2.3562 3.1416Pro Tip: linspace for Precision
Section titled “Pro Tip: linspace for Precision”When you need a specific number of points between two values, linspace is often a better choice than the colon operator to avoid floating-point inaccuracies.
% Create exactly 9 points from 0 to piv4 = linspace(0, pi, 9)% ans = 0 0.3927 0.7854 1.1781 1.5708 1.9635 2.3562 2.7489 3.1416Indexing Arrays and Matrices
Section titled “Indexing Arrays and Matrices”This is the most powerful application of the colon operator. It allows for the selection of entire rows, columns, or sub-arrays without writing loops.
Let’s use the following matrix A for our examples:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12]% A =% 1 2 3 4% 5 6 7 8% 9 10 11 12| Syntax | Purpose | Example | Result |
|---|---|---|---|
A(i, :) | Selects the entire i-th row. | A(2, :) | [5 6 7 8] |
A(:, j) | Selects the entire j-th column. | A(:, 3) | [3; 7; 11] |
A(r1:r2, c1:c2) | Selects a sub-matrix from rows r1-r2 and columns c1-c2. | A(2:3, 3:4) | [7 8; 11 12] |
A(:, c1:c2) | Selects all rows for a specific range of columns. | A(:, 2:3) | [2 3; 6 7; 10 11] |
A(end, :) | The end keyword refers to the last row or column. | A(end, :) | [9 10 11 12] |
A(:) | Linear Indexing: Selects all elements and reshapes them into a single column vector (column-major order). | A(1:4) | [1; 5; 9; 2] |
Best Practices: Vectorization and Logical Indexing
Section titled “Best Practices: Vectorization and Logical Indexing”Vectorization is the practice of using colon notation and other array operators to avoid loops, which is significantly faster and more readable in MATLAB.
% Slow, non-idiomatic wayfor i = 1:100 y(i) = sin(i/10);end
% Fast, vectorized wayx = 1:100;y_fast = sin(x/10);Logical Indexing is a related concept where you use a logical array (containing true and false values) to index into another array. This is extremely powerful for filtering data.
%% Using the matrix A from beforeA = [1 2 3 4; 5 6 7 8; 9 10 11 12];
%% Find all elements in A that are greater than 7large_elements = A(A > 7)% ans =% 9% 10% 8% 11% 12
%% Replace all odd numbers in A with 0A(mod(A, 2) == 1) = 0% A =% 0 2 0 4% 0 6 0 8% 0 10 0 12