You are on page 1of 7

CE4006/7006

Matrix Handout

University of Missouri

MATRICES
The matrix A is represented using the methodology, where and represent an elements row
and column location, respectively

11

[] = 21
31
41

12
22
32
42

13
23
33
43

14
24
34
44

In this case, [] is classified as a square matrix because the number of rows and columns are
equal. Square matrices have properties that are useful for numerical methods. This document
presents a brief review of MATLAB matrix formulation commands and presents special cases of
square matrices.

FORMING VECTORS AND MATRICES


Create a row vector (one dimensional matrix) using commas or spaces:
a=[11,12,13,14];

OR
a=[11 12 13 14];

a =
11

12

13

14

Create a column vector using semicolons:


a=[11;12;13;14];
a =

11
12
13
14

CE4006/7006

Matrix Handout

University of Missouri

Create a two-dimensional array (Matrix) using commas (or spaces) and semi-colons:
A=[11,12,13,14;21,22,23,24; 31,32,33,34;41,42,43,44]

OR
A=[11 12 13 14;21 22 23 24; 31 32 33 34;41 42 43 44]
A =

11

12

13

14

21

22

23

24

31

32

33

34

41

42

43

44

A matrix full of zeros can be formed using zeros(rows,columns) function..This is useful for
pre-allocating matrices to maximize efficiency.
>> zeros(3,6)

ans =

Similarly, a matrix of ones can be created using:


>> ones(3,3)

ans =

CE4006/7006

Matrix Handout

University of Missouri

USING VECTORS AND MATRICES


Call an element within a vector using location in parenthesis. Example:
>> a=[11 12 13 14];
>> a(3)

ans =

13
>> a(1:3)

ans =

11
12
13

Access elements within a matrix using i,j notation for rows and columns, respectively. Example:
A(1,1) returns a value of 11; while, A(3,4) returns a value of 34.

Using a colon in the row location indicates that you want all rows. Example:
>> A(:,1)

ans =

11
21
31
41

CE4006/7006

Matrix Handout

University of Missouri

A colon in the column location indicates you want to return all columns. Example:
>> A(1,:)

ans =

11

12

13

14

To transpose original matrix A:


>> transpose(A)

ans =

11

21

31

41

12

22

32

42

13

23

33

43

14

24

34

44

11

21

31

41

12

22

32

42

13

23

33

43

14

24

34

44

OR

>> A'

ans =

Also works for row and column vectors

To calculate the inverse of a matrix use inv(A)function..

CE4006/7006

Matrix Handout

University of Missouri

SPECIAL SQUARE MATRICES


SYMMETRIC MATRIX -- = for all and

5 1 2
[] = 1 3 7
2 7 8

DIAGONAL MATRIX -- All non-diagonal elements are zero.


11
0
[] =
0
0

0
22
0
0

0
0
33
0

0
0

0
44

A=zeros(4,4); %pre-allocate the full-size matrix filled with zeros to


increase efficiency of code
diagonal=[11,22,33,44]; %diagonal elements
for i=1:length(diagonal) %selects each diagonal element individually
A(i,i)=diagonal(i); %allocates each diagonal element to appropriate
location
end

IDENTITY MATRIX Diagonal matrix where all diagonal elements equal one. Formulated in
MATLAB using eye function. For this example:

1
0
[] =
0
0

>> A=eye(4,4)

0
1
0
0

A =

0
0
1
0

0
0

0
1

CE4006/7006

Matrix Handout

University of Missouri

UPPER TRIANGULAR MATRIX All elements below diagonal are zero. Can formulate in
MATLAB by setting values of existing matrix to zero using a for loop:

11
0
[] = 0
0

12
22
0
0

13
23
33
0

14
24
34
44

for i=1:size(A,1) %size(A,1) gives number of rows of A


for j=1:size(A,2) %size(A,2) gives number of columns of A
if j<i
A(i,j)=0;
end
end
end

LOWER TRIANGULAR MATRIX All elements above diagonal are zero. Can formulate in
MATLAB by setting values of existing matrix to zero using a for loop:

11

[] = 21
31
41

0
22
32
42

0
0
33
43

0
0

0
44

for i=1:size(A,1) %size(A,1) gives number of rows of A


for j=1:size(A,2) %size(A,2) gives number of columns of A
if j>i
A(i,j)=0;
end
end
end

BANDED MATRIX All elements, except band around diagonal, equal to zero. Size of band
is called bandwidth. For the example, bandwidth=3. Most of the time, you will place the
numbers into a matrix in the banded pattern (e.g. stiffness matrix in finite element) instead of
manipulating an existing matrix.

11

[] = 21
0
0

12
22
32
0
6

0
23
33
43

0
0

34
44

CE4006/7006

Matrix Handout

University of Missouri

Example code to build a similar matrix (not general enough to be useful but shows logic of
element allocation)
A=zeros(4,4); %pre-allocate the full-size matrix filled with zeros to
increase efficiency of code
diagonal=[11,22,33,44]; %diagonal elements
lowerband=[21,32,43]; %elements below diagonal
upperband=[12,23,34]; %elements above diagonal
for i=1:length(diagonal) %selects each diagonal element individually
A(i,i)=diagonal(i); %allocates each diagonal element to appropriate
location
end
for i=1:length(lowerband) %selects each diagonal element individually
A(i+1,i)=lowerband(i); %allocates lowerband elements to location below
diagonal
end
for i=1:length(upperband) %selects each diagonal element individually
A(i,i+1)=upperband(i); %allocates lowerband elements to location above
diagonal
end

You might also like