Vectors and Matrices in MATLAB

Contents

Defining vectors and matrices

A row vector is created with the command

b = [1 2 3 4]
b =

     1     2     3     4

Other possibilities include

b = 1:4
b =

     1     2     3     4

or

b = linspace(1,5,4)
b =

    1.0000    2.3333    3.6667    5.0000

A column vector is obtained by typing

c = [2; 4; 6]
c =

     2
     4
     6

or

c = [2 4 6]'
c =

     2
     4
     6

A matrix can be defined by typing

A = [1 2 3
    2 3 4
    5 6 7
    8 9 0]
A =

     1     2     3
     2     3     4
     5     6     7
     8     9     0

or

A = [1 2 3; 2 3 4; 5 6 7; 8 9 0]
A =

     1     2     3
     2     3     4
     5     6     7
     8     9     0

Some special matrices

B = zeros(4,2)
B =

     0     0
     0     0
     0     0
     0     0

E = eye(3)
E =

     1     0     0
     0     1     0
     0     0     1

C = ones(3,2)
C =

     1     1
     1     1
     1     1

Now we can define a block matrix

D = [A B; C E]
D =

     1     2     3     0     0
     2     3     4     0     0
     5     6     7     0     0
     8     9     0     0     0
     1     1     1     0     0
     1     1     0     1     0
     1     1     0     0     1