Vectors and Matrices


MATLAB types

Tend to think of them as:

Number (scalar)


String

Matrix/vector


Really, only one MATLAB type: arrays (matrices)


Creating vectors

a = [ 3, 5, 2, 9, 0 ]

b = [ 6; -8; 109 ]

c = 1 : 6

d = [ 10 : 5 : 200 ]

e = [ 4 9:3:16 22 21:-2:11 ]

f = [ a c a ]

c(10) = 17

g(4) = 3

h = b'

Creating matrices

m1 = [ 1 2 3; 4 5 6 ]


m2 = [ 1:5; 2:2:10; 3:3:15 ]


m3 = m1'


m1(3, 5) = 14


m4(4, 2) = 6




Built-in MATLAB functions

diag(1:4) 



diag(m2) 



ones(4, 3)



zeros(2, 6)



eye(4)



eye(2, 5)



length(c)
length(m2)


size(b)
size(m3)


Accessing vectors and matrices

A = [ 1:5 ; 6:10 ; 11:15 ; 16:20 ]






A( 3 , : )



A( 2, 2:4 )



A( [2 1 4], [2:4, 3, 1] )