MATLAB Lesson 3 - Vectors

The colon operator

Frequently you want to create vectors whose elements are defined by a simple expression or to access a sequence of elements in a vector. The colon operator : provides a very convenient way of doing this.

In its simplest form a:b starts at a, then increases in steps of 1, until b is reached.

Create a row vector of the integers from 5 to 11.

In this case the square brackets are not required to create a row vector.

>>  x = 5:11

 

A slightly more general form of the colon operator is a:step:b, which starts at a, then adds step repeatedly, until b is reached (or exceeded).

If step > 0, then a should be less than or equal to b, and the final number is less than or equal to b.

Create a row vector y of the integers between 5 and 11 with a step of 2.

This produces 5, 7, 9, 11.

>>  y = 5:2:11

In fact the value of step does not have to be an integer. It can even be negative, in which case a should be greater than b.

Create a vector z with elements zk = 1 + 0.05 k, for k = 0,1,2,...,20. How many elements does the vector z have?

The vector z has 21 elements 1, 1.05, 1.1, 1.15, ... 1.95, 2.

>>  z = 1 + 0.05*[0:20]

Alternatively, using the more general form of the colon operator

The vector z has 21 elements 1, 1.05, 1.1, 1.15, ... 1.95, 2.

>>  z = 1:0.05:2

In reverse order, starting with 2, taking steps of -0.05, until 1 is reached.

The vector zr has 21 elements 2, 1.95, 1.90, 1.85, ... 1.05, 1.

>>  zr = 2:-0.05:1

 

The linspace command

The task of creating a vector of equally (or linearly) spaced points between two limits occurs so commonly that MATLAB has a special command linspace to do this. The command linspace(a, b, n) creates n equally spaced points between a and b, including both a and b.

What do the following MATLAB commands produce?

As both end points are included, this produces the vector with elements 1.0000 1.1111 1.2222 1.3333 1.4444 1.5556 1.6667 1.7778 1.8889 2.0000.

If you wanted points with a spacing of 0.1, and including both end points, then you need a total of 11 points.

>>  z1 = linspace(1, 2, 10)

>>  z2 = linspace(1, 2, 11)

 

Indexing vectors

The colon operator is very helpful for indexing elements of a vector.

Find the first 4 elements of the vector z2

1:4 gives 1, 2, 3, 4, so you get the values of the first 4 elements of z2.

>>  z2(1:4)

Find the 7th, 9th and 11th elements of the vector z2

7:2:11 gives 7, 9, 11, so you get the values of these elements of z2.

>>  z2(7:2:11)

 

The MATLAB keyword end is used for several purposes. When referring to an element of a vector, it refers to the last element.

Find the last three elements of the vector z2.

end-2:end gives you elements 9, 10 and 11 of z2 as z2 has 11 elements.

>>  z2(end-2:end)

 

Warnings

Self-test Exercise

Use the colon operator to:

  • Create a vector u with elements uk = k, k = 1,...,12.
  • Create a vector v consisting of elements 1, 4, 7, 10 of u.

Answer:

  • u = 1:12
  • v = u([1:3:10])
Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

Equally spaced points can be created either by using the colon operator : or the linspace command

The colon operator is useful both for efficiently creating a new vector and for accessing selected elements of an already existing vector.