MATRICES AND MATRIX OPERATIONS: Unit 2
Dr. Wlodzislaw Kostecki
The Papua New Guinea University of Technology (PNGUT)
Department of Electrical and Communication Engineering
Lae, Morobe Province
Papua New Guinea
Copyright © 2000 by Wlodzislaw Kostecki
All rights reserved
-------------------------------------------------------------------
(2) Extracting components of matrices
OBJECTIVES :
• To provide alternative methods of extracting individual elements and the various parts of a matrix.
• To introduce the concepts of a sequence and set of elements and of sequence of lists .
• To introduce the concept of subscript notation and show its application.
• To show how to convert (row) vectors to column matrices .
• To show how to create new matrices using extracted parts of rows or complete rows of a matrix.
• To show how to create new matrices using extracted parts of columns or complete columns of a matrix.
> restart : with(linalg, augment, col, coldim, concat, delcols, delrows, row, rowdim, stackmatrix, submatrix, subvector, transpose) :
There are many situations where the various components of a matrix are needed for further mathematical treatment. This may include extracting all matrix elements, but most often desired are specified individual elements, parts of rows or columns, entire rows or columns, a number of consecutive rows or columns, and submatrices. All of them may be extracted from a given matrix using suitable functions and operations. Extracting components of matrices is also necessary in some other Units. An overview of the pertinent methods is given hereunder.
For example, consider a
(
×
)
matrix [
A
] given as
> A := matrix(4, 4, [a[11], a[12], a[13], a[14], a[21], a[22], a[23], a[24], a[31], a[32], a[33], a[34], a[41], a[42], a[43], a[44]]) : A = matrix(A) ;
A . Extracting all entries of a matrix
Method 1 . Returning matrix elements in the form of a sequence in which the elements are ordered row-by-row:
> elements(A) := op(1..rowdim(A)*coldim(A), convert(A, set)) : elements(A) ;
Method 2 . Returning matrix elements in the form of a (row) vector in which the elements are enclosed in brackets and ordered row-by-row:
> elements(A) := convert(A, vector) : elements(A) ;
[ Refer to Unit (5) for more information about the vector . ]
Method 3 . Returning matrix elements in the form of a set in which the elements are enclosed in braces and ordered row-by-row:
> elements(A) := convert(A, set) : elements(A) ;
Method 4 . Returning matrix elements in the form of a list in which the elements are enclosed in brackets and ordered row-by-row:
> elements(A) := convert(convert(A, set), list) : elements(A) ;
Method 5 . Returning matrix elements in the form of a list of lists in which each list comprises the elements of one row, the lists being enclosed in brackets and ordered row-by-row:
> elements(A) := convert(A, listlist) : elements(A) ;
Method 6 . Returning matrix elements in the form of an unordered sequence of lists in which each list contains one bracketed element:
> elements(A) := entries(A) : elements(A) ;
B. Extracting individual entries of a matrix
To extract specified entries of a matrix, use the subscript notation indicating the element location.
For example, extract the elements at locations (1,1) and (2,3) of matrix [ A ]:
> el[11] := A[1,1] : el[23] := A[2,3] : element[11](A) = el[11] ; element[23](A) = el[23] ;
C. Extracting part of a row of a matrix
To extract specified part of a row of a matrix, use the subvector function with a number indicating the row location, followed by number range indicating locations of desired columns.
For example, extract elements
...
of row
and elements
...
of row
of matrix [
A
]:
> Row[2][1..3](A) := subvector(A, 2, 1..3) : Row[4][2..3](A) := subvector(A, 4, 2..3) :
> 'Row[2][1..3](A)' = Row[2][1..3](A) ; 'Row[4][2..3](A)' = Row[4][2..3](A) ;
To create the corresponding row matrices , each consisting of elements of parts of the selected rows, use the matrix function:
> RM[2][1..3] := matrix(1, 3, [Row[2][1..3](A)]) : RM[4][2..3] := matrix(1, 2, [Row[4][2..3](A)]) :
> RM[Row[2]][1..3](A) = matrix(RM[2][1..3]) ; RM[Row[4]][2..3](A) = matrix(RM[4][2..3]) ;
D. Extracting part of a column of a matrix
To extract specified part of a column of a matrix, use the subvector function with a number range indicating locations of desired rows, followed by a number indicating the column location.
For example, extract elements
...
of column
and elements
...
of column
of matrix [
A
]:
> Col[2][2..3](A) := subvector(A, 2..3, 1) : Col[4][1..3](A) := subvector(A, 1..3, 4) :
> 'Col[2][2..3](A)' = Col[2][2..3](A) ; 'Col[4][1..3](A)' = Col[4][1..3](A) ;
To convert the specified parts of columns into column matrices , each consisting of elements of the selected part of the column, use the convert and matrix functions:
> CM[2][2..3] := convert(Col[2][2..3](A), matrix) : CM[4][1..3] := convert(Col[4][1..3](A), matrix) :
> CM[Col[2]][2..3](A) = matrix(CM[2][2..3]) ; CM[Col[4]][1..3](A) = matrix(CM[4][1..3]) ;
E. Extracting individual rows of a matrix
To extract specified rows of a matrix, use the row function with a number indicating the row location. The output is a (row) vector .
For example, extract rows
and
of matrix [
A
]:
> Row[1](A):=row(A, 1) : Row[3](A):=row(A, 3) : 'Row[1](A)' = Row[1](A) ; 'Row[3](A)' = Row[3](A) ;
To create the corresponding row matrices , each consisting of elements of the selected row, use the matrix function:
> RM[1] := matrix(1, 4, [Row[1](A)]) : RM[3] := matrix(1, 4, [Row[3](A)]) :
> RM[Row[1]](A) = matrix(RM[1]) ; RM[Row[3]](A) = matrix(RM[3]) ;
F. Extracting individual columns of a matrix
To extract specified columns of a matrix, use the col function with a number indicating the column location. The output is a (row) vector .
For example, extract columns
and
of matrix [
A
]:
> Col[1](A) := col(A, 1) : Col[3](A) := col(A, 3) : 'Col[1](A)' = Col[1](A) ; 'Col[3](A)' = Col[3](A) ;
To convert the specified columns into column matrices , each consisting of elements of the selected column, use the convert and matrix functions:
> CM[1] := convert(Col[1](A), matrix) : CM[3] := convert(Col[3](A), matrix) :
> CM[Col[1]](A) = matrix(CM[1]) ; CM[Col[3]](A) = matrix(CM[3]) ;
G. Extracting several consecutive rows of a matrix
To extract several consecutive rows of a matrix, use the row function with a number range indicating the row locations. The output is a sequence of (row) vectors .
For example, extract rows
...
of matrix [
A
]:
> Rows[2..3](A) := row(A, 2..3) : 'Rows[2..3](A)' = Rows[2..3](A) ;
To create a matrix [ B ] consisting of the selected rows, use either of the following methods.
Method 1 . Using conversion of (row) vectors into row matrices and the stackmatrix function:
Step 1 . Extract individual vectors from the above sequence of vectors using the subscript notation and then, convert either of them into a row matrix using the matrix function:
> RM[Row[2]] := matrix(1, 4, [Rows[2..3](A)[1]]) : RM[Row[3]] := matrix(1, 4, [Rows[2..3](A)[2]]) :
> 'RM[Row[2]]'(A) = matrix(RM[Row[2]]) ; 'RM[Row[3]]'(A) = matrix(RM[Row[3]]) ;
Step 2 . Use the row matrix with the highest row index ( RM[Row[3]] in this case) as the reference and stack a row matrix with the lower row index ( RM[Row[2]] in this case) directly on top of the reference matrix by employing the stack function:
> B := stackmatrix(RM[Row[2]], RM[Row[3]]) : B = matrix(B) ;
N.B. The function stackmatrix joins two or more matrices or vectors vertically.
Method 2 . Using (row) vectors extracted from the above sequence of vectors, typed in a proper order in brackets under the matrix function:
> B := matrix(2, 4, [Rows[2..3](A)[1], Rows[2..3](A)[2]]) : B= matrix(B) ;
H. Extracting several consecutive columns of a matrix
To extract several consecutive columns of a matrix, use the col function with a number range indicating the column locations. The output is a sequence of (row) vectors .
For example, extract columns
...
of matrix [
A
]:
> Cols[3..4](A) := col(A, 3..4) : 'Cols[3..4](A)' = Cols[3..4](A) ;
To create a matrix [ C ] consisting of the selected columns, use either of the following methods.
Method 1 . Using conversion of (row) vectors into column matrices and the augment function:
Step 1 . Extract individual vectors from the above sequence of vectors using the subscript notation and then, convert either of them into a column matrix using the convert and matrix functions:
> CM[Col[3]] := convert(Cols[3..4](A)[1], matrix) : CM[Col[4]] := convert(Cols[3..4](A)[2], matrix) :
> 'CM[Col[3]]'(A) = matrix(CM[Col[3]]) ; 'CM[Col[4]]'(A) = matrix(CM[Col[4]]) ;
Step 2 . Use the column matrix with the highest column index ( CM[Col[4]] in this case) as the reference and join a column matrix with the lower column index ( CM[Col[3]] in this case) directly to the left of the reference matrix by employing either the augment or concat function:
> C := augment(CM[Col[3]], CM[Col[4]]) : C := concat(CM[Col[3]], CM[Col[4]]) :
> C = matrix(C) ;
N.B. The function augment or its synonym concat joins two or more matrices or vectors together horizontally.
Method 2 . Using conversion of (row) vectors into row matrices and the stackmatrix and transpose functions:
Step 1 . Extract individual vectors from the above sequence of vectors using the subscript notation and then, convert either of them into a row matrix using the matrix function:
> RM[Col[3]] := matrix(1, 4, [Cols[3..4](A)[1]]) : RM[Col[4]] := matrix(1, 4, [Cols[3..4](A)[2]]) :
> 'RM[Col[3]]'(A) = matrix(RM[Col[3]]) ; 'RM[Col[4]]'(A) = matrix(RM[Col[4]]) ;
Step 2 . Use the row matrix with the highest column index ( RM[Col[4]] in this case) as the reference and stack a row matrix with the lower column index ( RM[Col[3]] in this case) directly on top of the reference matrix by employing the stackmatrix function. Then, interchange the rows and columns of the resultant matrix using the transpose function:
> C := transpose(stackmatrix(RM[Col[3]], RM[Col[4]])) : C = matrix(C) ;
[ For the transpose of a matrix, refer to Unit (10). ]
I. Extracting a submatrix of a matrix
To extract a submatrix containing the elements specified by the row range and the column range, use the submatrix function with a number range indicating row locations, followed by a number range indicating column locations..
For example, extract a submatrix contained between rows
...
and columns
...
of matrix [
A
]:
> S := submatrix(A, 2..3, 2..4) : S = matrix(S) ;
N.B. The same result may be obtained using a suitable combination of the functions delcols , which del etes col umn s and delrows , which del etes rows of a matrix. A combination appropriate for this particular case is
> S := delrows(delrows(delcols(A, 1..1), 1..1), 3..3) : S = matrix(S) ;
* * *
Proceed to Unit (3) for " Addition and subtraction of matrices ".
-------------------------------------------------------------------