MATLAB Lesson 3 - Vectors

Vector arithmetic

The standard vector operations of adding two vectors and multiplying a vector by a scalar work in MATLAB.

However the straight forward multiplication or division of vectors is not defined.

Addition of two vectors

The sum of two vectors of the same size is obtained by adding corresponding elements.

The vector u has 3 elements 1, 2, 3.

The vector v has 3 elements 10, 11, 12.

The vector w has 3 elements 11, 13, 15.

>>  u = [1 2 3]
>>  v = [10 11 12]
>>  w = u + v

MATLAB can handle vectors with any number of elements, even hundreds of thousands of elements. However both vectors must have the same number of elements for their sum to be defined.

Try adding vectors of different sizes.

The vector u has 5 elements 1, 2, 3.

The vector v has 6 elements 10, 11, 12, 13.

??? Error using ==> plus
Matrix dimensions must agree.

>>  u = [1:3]
>>  v = [10:13]
>>  w = u + v

A vector times a scalar

Multiplying a vector by a scalar produces another vector of the same size in which each element of the original vector has been multiplied by the scalar.

Calculate w = -2u, where u is defined above.

The vector u has 3 elements 1, 2, 3 from before, so the vector w has elements -2, -4, -6

>>  w = -2 * u

 

Adding a scalar to a vector

One operation where MATLAB is different from standard mathematics is that a scalar may be added to any vector producing a new vector with the scalar added to each element of the vector.

Calculate w = -2 + u, where u is defined above.

The scalar -2 is added to each element of the vector u with elements 1, 2, 3, so the vector w has elements -1, 0, 1

>>  w = -2 + u

 

Element by element operations

Sometimes it is very useful to apply arithmetic operations to each element of a vector (or matrix). These element by element operations are

Note that addition (subtraction) of two vectors and multiplication of a vector by a scalar are already defined to apply element by element.

Calculate xi = i2 for i = 1, 2, 3, 4, 5 firstly using multiplication, then using powers.

Define the vector u with elements 1, 2, 3, 4, 5

Element by element multiplication .* to get 1, 4, 9, 16, 25

Element by element powers .^ to get 1, 4, 9, 16, 25

>>  u = [1:5]
>>  x1 = u .* u

>>  x2 = u.^2


Calculate the vector z with elements 1/i for i = -4,-3,..,3,4. What is the 5th element of z?

Define the vector u with elements -4,...,4

Element by element division ./

The fifth element is 1/0 which in MATLAB gives Inf

>>  u = [-4:4]
>>  z = 1./u
>>  z(5)

Many MATLAB functions, for example exp and sqrt, work with vectors, applying the function to each element of the vector.

 

Warnings

Self-test Exercise

Create a vector u with elements 1 / j3 for j = 2,6,10,...,100. What is the last element of u?

Answer:

  • u = 1 ./ [2:4:100].^3
  • u(end)
Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

The standard vector arithmetic operations of adding two vectors of the same size or multiplying a vector by a scalar can be done in MATLAB.

MATLAB also has additional vector operations of adding a scalar to each element of a vector, and elementwise operators .* for multiplication, ./ for division and .^ for powers.