MATLAB Lesson 3 - Vectors

Row vectors

In MATLAB you can create a row vector using square brackets [ ]. Elements of the vector may be separated either by one or more blanks or a comma ,.

Create a row vector x with elements x1 = 1, x2 = -2 and x3 = 5.

Square brackets are use to create a row vector.

The elements may be separated either by blanks or commas.

>>  x = [1 -2 5]
>>  x = [1, -2, 5]

 

The elements of a vector may be the result of arithmetic operations.

Create a row vector y with elements 210, 212, 214.

Square brackets are use to create a row vector, whose elements may be MATLAB expressions.

The elements may be separated by commas or blank spaces.

>>  y = [2^10, 2^12, 2^14]


 

Elements of vectors

To refer to elements in a vector MATLAB uses round brackets ( ). For example, the second element x2, is referenced by x(2). This can be used either to get the value of an element or to assign a value to an element of a vector.

Find the value of y2 in the previous example, then change its value to -1. What is displayed in the command window?

y(2) displays the value of the second element of the vector stored in the variable y .

This assigns the value -1 to the second element of y.

Note that the whole vector y is echoed to the command window, even though only the second element was changed.

>>  y(2)
>>  y(2) = -1



 

Accessing several elements of a vector

The elements of a vector are indexed, starting with 1 continuing to the length of the vector, just as in mathematics.

MATLAB uses vectors of integers between 1 and the length of a vector to refer to several elements of a vector at once. For example x([2 3]) refers to the second and third elements x2, x3 of x.

Use one MATLAB command to display the first and third elements of the vector x.

This displays elements 1 and 3 of the vector x, assuming x has already been defined. Note that there is a blank between the 1 and the 3, so it is not x([13]).

Although a space is commonly used to separate elements, to make it clearer a comma can be used to separate the 1 and the 3.

>>  x([1 3])
>>  x([1, 3])



 

Create a vector sample1 with elements consisting of the first, third, second and first (again) elements of x in that order.

i2 is a vector with elements 1, 3, 2, 1

This produces a vector consisting of the elements of x corresponding to the values in i2 and assigns the result to a variable called sample1.

>>  i2 = [1 3 2 1]
>>  sample1 = x(i2)

 

Number of elements in a vector

The number of elements in an array (for instance a vector) can be found using the MATLAB command numel.

Find the number of elements of the vector sample1 in the previous exercise.

The vector has four elements.

>>  numel(sample1)

The length of a vector can also be found using the MATLAB command length. For vectors this is the same as numel.

Find the length of the vector sample1 in the previous exercise.

The vector has four elements.

>>  length(sample1)

 

Warnings

Self-test Exercise

Create a vector u with elements k2, k = 6,...,12, then create a vector v consisting of elements 1, 3, 5, 7 of u. Check that v has length 4.

Answer:

  • u = [6^2 7^2 8^2 9^2 10^2 11^2 12^2]
  • v = u([1 3 5 7])
  • numel(v)
Use the mouse to select the text between the word "Answer" and here to see the answer.

We will see a much easier way to construct u and v after the next two modules!

Summary

Row vectors are created using square brackets [ ], with blank spaces or commas to separate elements.

Element k of a vector u is accessed as u(k), where k is an integer between 1 and numel(u).