You are on page 1of 46

Operations on Matrices

(Matrices 3)

Dominic C. Ezugworie
chidominez@gmail.com
+234 (0) 703 9071904

© 2012 Dominic C. Ezugworie


Outline

2
Resizing Matrices
• Expanding the size of a matrix
– Concatenating matrices
– Storing outside matrix bounds
• The matrix must remain rectangular

• Recommended to Preallocate space instead

3
Resizing Matrices
• Concatenating matrices
A = [4,5,6;7,3,1]; B = [5,9,3;8,2,1];
C = 4*rand(2,4); D = 2*ones(1,3);
• Vertically
A = [A;B;D]
• Horizontally
B = [A,B,C];
• Other concatenation functions still apply

4
Expanding a Matrix
• Storing outside matrix bounds
– Adding smaller blocks to a matrix
– Matrix padded with zeros to keep rectangular

% Storing a single element

Padded with zeros


5
Expanding a Matrix
• Storing outside matrix bounds
– Adding smaller blocks to a matrix
– Matrix padded with zeros to keep rectangular

% Storing a multiple elements

Padded with zeros


6
Your turn
• Given the matrices below
– Concatenate them diagonally
• Matrix padded with zeros to keep rectangular

7
Resizing Matrices
• Diminishing matrix size
– Assign the empty matrix [] to the row or column
you want to remove

8
Diminishing matrix size
• Assign [] to linear index/indices
– converts to row vector

9
Reshaping Matrices
• Change the sizes of the dimensions
• Popular reshape function
– reshape
• Rotate or flip a matrix
– rot90, fliplr, flipud, flipdim
• Transpose a matrix
– transpose, ctranspose

10
Reshaping Matrices (functions)

11
Reshaping Matrices – Notes
• MATLAB operates column wise
• The matrix must be a 2-D matrix

12
Outline

13
Shifting Matrices
• Shift the values circularly
– circshift
• Swap quadrants
– fftshift
• Shift the dimensions
– shiftdim
• Rearrange the order of dimensions
– permute, ipermute
14
Shifting Matrices
Shift values of array circularly
B = circshift(A,shiftsize)
– A and B are matrices of same size
– shiftsize is a vector of integers
• nth element specifies shift amount for nth dimension
• Positive shift = shifted down/right
• Negative shift = shifted up/left
• What are the first and second dimensions of
a matrix?
15
Shifting Matrices
Shift values of array circularly

16
Your turn 1
Create a 5-by-8 matrix named A and shift it along both
dimensions: three columns to the right and two rows up

Example
Matrix :

Answer:

17
Your turn 2
Example
Matrix :

Answer:

Since circshift circulates shifted rows and columns around


to the other end of a matrix, shifting by the exact size of A
returns all rows and columns to their original location

18
Your turn 3
Generate the checkerboard pattern
Sorting Matrices
B = sort(A) % default modus operandi
• sorts the elements along different dimensions of an
array, and arranges those elements in ascending
order

20
Sorting Matrices
• sort(A,1) % Sort along columns, ascending

• sort(A,2) % Sort along rows, ascending

• sort(...,’descend’) % Sort..., descending

• [B, IX] = sort(A,...) % also returns a


% permutation array of indices in IX

• issorted(A) % determines whether set


% elements are in sorted order

21
Sorting Matrices

22
Sorting Matrices

23
Sorting Matrices – sortrows
• Sorts the rows based on a particular column
– Default is first column
– E.g. B = sortrows(A)
• Familiar dictionary sort for strings
• sortrows(A, column)
– Sorts based on the column(s) specified in vector
column
– Positive column element sorts ascending
– Negative column element sorts descending
24
Sorting Matrices – sortrows
• Default operation
sortrows(A)
• bases the sort on the first column of the
matrix.
• For any rows that have equal elements in a
particular column, sorting is based on the
column immediately to the right

25
Sorting Matrices – sortrows

26
Sorting Matrices – sortrows

27
Outline

28
Operations on Diagonal Matrices
• Return matrix diagonals
– diag
• Construct a diagonal matrix
– diag % simple diagonal matrix
– blkdiag % block diagonal matrix
• Return the triangular part of a matrix
– tril % lower triangular part
– triu % upper triangular part

29
Return Matrix Diagonals
• A = magic(5); % create a 5-by-5 matrix

• D1 = diag(A); % returns contents of


% main diagonal

• D2 = diag(A,k); % returns contents of


% kth diagonal
 k = 0 => represents the main diagonal
 k > 0 => above the main diagonal
 k < 0 => below the main diagonal

30
Construct a diagonal matrix
Simple diagonal matrix
• A = diag(v); % elements on the main diagonal
• A = diag(v,k); % elements on the kth diagonal

Block diagonal matrix – concatenating matrices diagonally


• D = blkdiag(A,B,C); % A, B, C are matrices
% they mustn’t be square nor of same size

31
Return triangular part of matrix
A = magic(7);
• From the main diagonal
tril(A) % lower triangular part
triu(A) % upper triangular part

• From an alternate diagonal


tril(A,-1)
triu(A,2)

32
Your turn
• Create a tridiagonal matrix

Try this
Outline

34
Empty matrices
• An empty matrix has one or more dimensions equal to
zero
A = []; % creates a 0-by-0 empty matrix
whos A % check the properties of A
• Create higher dimension empty matrices with the
special matrix functions e.g.
B = zeros(0,5) % creates a 0-by-5
% empty matrix
• Most operations on empty matrices return empty
matrices as well

35
Full and sparse matrices
• Consider a matrix with large number of
zero-valued elements
• Full matrix: when each element has
respective memory allocation
• Sparse matrix: when the zero-valued
elements are suppressed in memory
 Let’s consider an example of 1100-by-1100 matrix
 Remember each double element occupies 8 bytes
in memory
36
Full and sparse matrices

37
Full and sparse matrices
What is the result?

38
Sparse matrices
• A way to store data with large percentage of
zero elements more efficiently
• Only non-zero elements with their row indices
are stored
• Reduces computational time and memory
storage requirements
• Created using the sparse function

39
Sparse matrices
Create sparse matrix
• sparse(m,n), speye, spdiags, spones
• sprand, sprandsym,

Convert to sparse matrix


• sparse(A), full(S)
Get info about a sparse matrix
• nnz, nonzeros, nzmax,
• find, issparse, spy

40
Sparse matrices
Other operations on sparse matrices
• Graph theory and Reordering algorithms
• Linear algebra and linear equations
• Well treated in the MATLAB documentation
MATLAB\User Guide\Mathematics\Sparse Matrices\Function Summary

• Also find there functions that do not support


sparse matrices

41
Are We Even Now ?
 Operations on Matrices (Matrices 3)

42
Exercise 1 – shift a matrix

I have this
matrix:

Write a program
to process it and
give me this:

43
Exercise 2a
• Write a program to automatically generate
this matrix

44
Exercise 2b
• Write a general program to automatically
generate the pattern of matrix in ex. 2a
• Your program should accept the number of
rows, say m, and the starting value at index
(1,1), say n.
• It will then generate an m-by-m matrix
following the pattern shown

45
Exercise 3
• Given a triangular matrix based main diagonal
• Write a program to produce a symmetric
matrix by filling the other part of the matrix
with the provided values
• Your program may determine which part of
the matrix (upper or lower) needs filling; so,
you may need to determine the type (lower or
upper) of triangular matrix provided
• Test with your matrix of ex. 2
46

You might also like