Contents

Matlab Vector exercises

A.J. Melhus  4/4/10
% When performing mathematical operations in Matlab, the word "vector"
% often comes up.  What exactly does "vector" mean to Matlab and why
% use them?

% A vector is simply a list of numbers.  In terms of linear algebra,
% vectos are 1 dimensional matrices - either a single-row or single-column
% matrix.  Vectors are important to Matlab because they are almost always
% the quickest and most efficient method for getting an answer.

% Matlab stands for "Matrix Laboratory," which means that Matlab is very
% good at vector and matrix mathematics.

Defining vectors

There are 3 basic ways to define a vector in Matlab:
% 1. Manually enter every vector element, using

x = [3, 5, 7, 9]
% (see below for what MATLAB makes of this command)

% This approach works for arrays of numbers too, with commas and/or
% spaces to separate entries and semicolons to separate rows:

x = [0, 1, 2; 3, 4, 5]

% 2. Use colon notation.  This method allows you to quickly generate
% series vectors with a pre-defined or user-specified element width.

% Syntax:
%     xstart:dx:xend
        % xstart is the first value of your vector
        % dx (optional) is the step width, the difference between two
        %    consecutive entries
        % xend is the last value of your vector

% A simple example showing that the default increment is 1
y1 = 1:5

% there are more entries here because the step size, dy, is smaller
y2 = 1:0.5:5

% 3. Use a built-in function to create vectors of specified size - zeros,
% ones, linspace, logspace.  These functions are helpful because they let
% you decide how many entries you want in the vector, and do the rest for
%y ou.

% The most useful for basic plotting is linspace.
  %Syntax:
  % linspace(xstart, xend, num_entries)
  % num_entries is the number of entries (or size) in your vector

% Here a vector from 0 to 3.8, with 10 equally-spaced entries:
y3 = linspace(0,3.8, 10)

% Initialize an array and fill with zeros
y4 = zeros(3)
x =

     3     5     7     9


x =

     0     1     2
     3     4     5


y1 =

     1     2     3     4     5


y2 =

    1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000    4.5000    5.0000


y3 =

         0    0.4222    0.8444    1.2667    1.6889    2.1111    2.5333    2.9556    3.3778    3.8000


y4 =

     0     0     0
     0     0     0
     0     0     0

Vector math

When doing vector mathematics, it is useful to understand how MATLAB performs operations with vectors. The important distinction here is the use of the dot, . This tells MATLAB to perform mathematical operations element-by-element, instead of normal linear algebra. Another important notation is the apostrophe, '. This tells Matlab to transpose the vector - change it from a row vector to a column vector or vice-versa. It is easiest if you do the following simple examples for yourself:

% Make a couple of example vectors
a = 1:5;
b = 6:10;
% The semicolon at the end keeps them from printing on the screen
% with the name "ans," short for answer.  You can see the values just
% by typing the variable name at the prompt, like:
a

% Now on to manipulations
% Addition (or subtraction) is automatically element-by-element
a + b

% Multiplication (and division) are trickier: they can go
% element-by-element or as vector operations.

% The dot before the multiplicaiton symbol makes the multiplication
% (in this case, or any operation in general) work element-by-element.
 a.*b
% Mulitplying without the dot here leaves MATLAB confused, because it's
% thinking how to multiply two vectors rather than element-by-element
%    a*b
% would give an error message.

% If you know about vector dot products, MATLAB will happily produce those,
% but first the vector dimensions must agree.  To do that, make b into a
% column vector
b'
% Then a*b' works, and gives the dot product:
a*b'
% Since the dot product is commutative, a*b' = b*a', as
b*a'

% For any of these operations to work, the vectors must have the same
% lengths.  Define a new vector c, which has more entries than a or b:
    c = 0:5
% Now
%   a.*c
% produces an error.
a =

     1     2     3     4     5


ans =

     7     9    11    13    15


ans =

     6    14    24    36    50


ans =

     6
     7
     8
     9
    10


ans =

   130


ans =

   130


c =

     0     1     2     3     4     5

Vectors in functions

When making functions and equations more complicated in nature, it is important to keep these rules in mind, to ensure Matlab is doing what you want it to.

% This works:
  x = 1:4
  (x+1)./x
% but
   (x+1)/x
% without the dot will only work if x is a single number, but not a vector
% -- you only get one number back (can you work out what it's done to get
% the answer here?).

% Fortunately, MATLAB's built-in functions like sqrt() or sin() know this
% and will work on vectors:
sin(x)
x =

     1     2     3     4


ans =

    2.0000    1.5000    1.3333    1.2500


ans =

    1.3333


ans =

    0.8415    0.9093    0.1411   -0.7568