Getting Started    

The Colon Operator

The colon, :, is one of the most important MATLAB operators. It occurs in several different forms. The expression

is a row vector containing the integers from 1 to 10

To obtain nonunit spacing, specify an increment. For example,

is

and

is

Subscript expressions involving colons refer to portions of a matrix.

is the first k elements of the jth column of A. So

computes the sum of the fourth column. But there is a better way. The colon by itself refers to all the elements in a row or column of a matrix and the keyword end refers to the last row or column. So

computes the sum of the elements in the last column of A.

Why is the magic sum for a 4-by-4 square equal to 34? If the integers from 1 to 16 are sorted into four groups with equal sums, that sum must be

which, of course, is


  Subscripts The magic Function