CHAPTER 2: MATRIX COMPUTATIONS AND LINEAR ALGEBRA

 

Lecture 2.2: Block and band matrices, structures and cells

 

Structure of a band matrix:

 

         diag(x): outputs a diagonal matrix with elements of x at the main diagonal

         diag(x,k): outputs a matrix with elements of x at the k-th diagonal, located above the main diagonal if k > 0 and below the main diagonal if k < 0

         diag(A,k): outputs the vector standing at the k-th diagonal of a matrix A

 

x = [ 1 2 3 4 5 ];

A1 = diag(x), A2 = diag(x,2), A3 = diag(x,-1)

v = diag(A2,2)

A1 = 1 0 0 0 0

0 2 0 0 0

0 0 3 0 0

0 0 0 4 0

0 0 0 0 5

 

A2 = 0 0 1 0 0 0 0

0 0 0 2 0 0 0

0 0 0 0 3 0 0

0 0 0 0 0 4 0

0 0 0 0 0 0 5

0 0 0 0 0 0 0

0 0 0 0 0 0 0

 

A3 = 0 0 0 0 0 0

1 0 0 0 0 0

0 2 0 0 0 0

0 0 3 0 0 0

0 0 0 4 0 0

0 0 0 0 5 0

 

v = 1

2

3

4

5

 

         tril(A) : extracts the lower triangular part of a matrix

         tril(A,p): extracts the lower triangular part of a matrix on and below the p-th diagonal

         triu(A) : extracts the upper triangular part of a matrix

         triu(A,q): extracts the upper triangular part of a matrix on and above the q-th diagonal

 

A = magic(5)

% fun matrix magic(N) is constructed from the integers

% 1 through N^2 with equal row, column, and diagonal sums

 

B1 = tril(A,1), B2 = triu(A,-1)

 

 

A =

17 24 1 8 15

23 5 7 14 16

4 6 13 20 22

10 12 19 21 3

11 18 25 2 9

B1 =

17 24 0 0 0

23 5 7 0 0

4 6 13 20 0

10 12 19 21 3

11 18 25 2 9

B2 =

17 24 1 8 15

23 5 7 14 16

0 6 13 20 22

0 0 19 21 3

0 0 0 2 9

 

Definition: A is a banded matrix that has lower bandwidth p and upper bandwidth q if A has zeros below (-p)-th diagonal and above q-th diagonal. If p = 0, A is an upper triangular matrix. If q = 0, A is a lower triangular matrix. If p = q = 0, A is a diagonal matrix. If p = q = 1, A is a tri-diagonal matrix.

 

% an example of a tridiagonal matrix for a three-point central difference:

% u''(x) = (u(x+h) 2*u(x) + u(x-h))/h^2

x = 1 : 7; % the step size is one, h = 1

n = length(x);

A1 = -2*diag(ones(1,n)) + diag(ones(1,n-1),1) + diag(ones(1,n-1),-1)

A2 = -2*eye(n) + triu(tril(ones(n),1),-1)-eye(n)

% A1 = A2, the same matrix is generated by two different MATLAB functions

A1 =

-2 1 0 0 0 0 0

1 -2 1 0 0 0 0

0 1 -2 1 0 0 0

0 0 1 -2 1 0 0

0 0 0 1 -2 1 0

0 0 0 0 1 -2 1

0 0 0 0 0 1 -2

 

A2 =

-2 1 0 0 0 0 0

1 -2 1 0 0 0 0

0 1 -2 1 0 0 0

0 0 1 -2 1 0 0

0 0 0 1 -2 1 0

0 0 0 0 1 -2 1

0 0 0 0 0 1 -2

 

Structure of a block matrix:

 

AA = : a block matrix consisting of four matrices A,B,C,D

Constraints: B must have the same number of rows as A, C must have the same number of columns as A, and D must have the same number of rows as C and the same number of columns as B.

 

A = [ 1 2 3 4 ; 1 2 3 4; 1 2 3 4; 1 2 3 4] % A is a 4-by-4 matrix

b = [ 0; 0; 1; 1 ] % b is a column vector

c = b'; % c is a row-vector

d = 100 % d is a scalar

AA = [ A, b; c, d ] % a block 5-by-5 matrix

 

A = 1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4

 

b = 0

0

1

1

 

d = 100

 

AA = 1 2 3 4 0

1 2 3 4 0

1 2 3 4 1

1 2 3 4 1

0 0 1 1 100

 

b1 = [ 0; 0; 1; 1; 1]; % b has now a mis-match in a number of columns

AA1 =[ A, b1; c, d ] % an error in building the block matrix

 

??? Error using ==> horzcat

All matrices on a row in the bracketed expression must have the

same number of rows.

 

         sub-elements of block matrices are lost after construction

 

AA(1,1) % returns only the first element of A, not the first block of AA

AA(1:4,1:4) % returns the first block of AA, i.e. the matrix A

 

ans =

1

ans =

1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4

 

 

BB = AA(1:2:5,1:3:5)

% the command returns a block of AA consisting of the first, third, and fifth rows of AA

% and the first and third columns of AA, i.e. the command returns a 3-by-2 matrix

 

BB = 1 4

1 4

0                    1

         sub-elements of block matrices can be created as cell arrays

 

CC = cell(2,2);

C{1,1} = A; C{1,2} = b; C{2,1} = c; C{2,2} = d;

C, C{1:2,1:2}

 

C =

[4x4 double] [4x1 double]

[1x4 double] [ 100]

ans =

1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4

ans =

0 0 1 1

ans =

0

0

1

1

ans = 100

 

Advanced data objects:

 

         struct: establish a structure array with fields and numerical or string values

 

PA = struct('point','A','x',5,'y',3) % creates point A(5,3)

PB = struct('point','B','x',1,'y',-1) % creates point B(1,-1)

 

PA = point: 'A'

x: 5

y: 3

PB = point: 'B'

x: 1

y: -1

 

 

PC.point = 'C'; PC.x = 9; PC.y = 4; % creates point C(9,4)

PC

 

PC =point: 'C'

x: 9

y: 4

 

 

P = struct('point',char('A','B','C'),'x',[5;1;9],'y',[3;-1;4])

% char('A','B','C') creates separate rows for character strings 'A','B', and 'C'

% different structures in P are located at different rows

 

P = point: [3x1 char]

x: [3x1 double]

y: [3x1 double]

 

a1 = PA.point, a2 = PC.x, a3 = PC.y

a4 = P.point(1), a5 = P.x(:)

r = sqrt(PA.x^2 + PA.y^2) % computes radius in polar coordinates

 

a1 = A

a2 = 9

a3 = 4

a4 = A

a5 = 5

1

9

r = 5.8310

 

 

         cell: a data container, that may contain any type of data (array of numbers, strings, structures, or cells)

 

C = cell(2,2);

C{1,1} = rand(3); % a 3-by-3 random matrix is in C{1,1} box

C{1,2} = char('john','dmitry'); % an array of 2 strings is in C{1,2} box

C{2,1} = PA; % a structure PA is in C{2,1} box

C{2,2} = cell(3,3); % a 3-by-3 nested cell is in C{2,2} box

 

 

C(1,1), C(1,2), C(2,1),C(2,2)

 

ans =

[3x3 double]

ans =

[2x6 char]

ans =

[1x1 struct]

ans =

{3x3 cell}

 

D = C{1,1}

 

D = 0.9501 0.4860 0.4565

0.2311 0.8913 0.0185

0.6068 0.7621 0.8214