0% found this document useful (0 votes)
180 views25 pages

Week 12 - Lecture Notes Special Matrices

The document provides lecture notes on special matrices in R including nilpotent, idempotent, and involutary matrices. It also covers topics like submatrices, basic matrix operations in R like addition, subtraction, and multiplication of matrices. Functions to work with matrices are discussed including creating, accessing elements, and binding matrices.

Uploaded by

Farheen Nawazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views25 pages

Week 12 - Lecture Notes Special Matrices

The document provides lecture notes on special matrices in R including nilpotent, idempotent, and involutary matrices. It also covers topics like submatrices, basic matrix operations in R like addition, subtraction, and multiplication of matrices. Functions to work with matrices are discussed including creating, accessing elements, and binding matrices.

Uploaded by

Farheen Nawazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Week 12 – Lecture notes

Special Matrices
Nilpotent Matrix
If A is a square matrix such that A n  0 where n
is a positive integer, then A is said to be nilpotent.
 4 2 
For example: If A=   then A 2
?
8 4 
 4 2   4 2  0 0 
A 
2
     0
8 4  8 4  0 0 
This implies that the given matrix A is nilpotent
matrix.

Idempotent Matrix
If A is a square matrix such that A2 =A×A=A
then A is said to be an idempotent matrix.
1 0 
For example: If I    then I 2
?
0 1 
1 0  1 0  1 0 
I 
2
     I
0 1  0 1  0 1 
This implies that the unit matrix I is an
idempotent matrix.

Involutary Matrix
If A is a square matrix such that A 2 =I then A is
said to be involutary matrix.
-1 0  2
For example: If A=   then A =?
 0 -1
-1 0  -1 0  1 0 
2
A =      I
 0 -1  0 -1 0 1 
This implies that the unit matrix I is an
involutary matrix.
Sub Matrix
Let A be any given matrix. If some rows or
columns or both of A are deleted from A, the
resulting matrix is called sub matrix of A.
4 3 2 1
For example: If A   1 0 3 4  then the
 
 3 5 1 2 
sub matrix by deleting 1st row and 1st column is
0 3 4
 5 1 2 
 

Check yourself on solving the questions given


below
 1 1 1
1. If A   3 3 3  then the matrix A is…
 
 5 5 5 
a) Nilpotent matrix b) Idempotent matrix
c) Involutary matrix d) none of these
 0 1 1
2. If A   4 3 4  then the matrix A is…
 
 3 3 4 
a) Nilpotent matrix b) Idempotent matrix
c) Involutary matrix d) none of these

1 1 3
3. If A   5 2 6  then the matrix A is…
 
 2 1 3
a) Nilpotent matrix b) Idempotent matrix
c) Involutary matrix d) unit matrix
Introduction to R Language
R is a computer language which is processed
by a special program called an interpreter. This
program reads and evaluates R language
expressions, and prints the values determined
for the expressions. R includes a suite of
operators for calculations on arrays, in
particular matrices. R is Free Software, and
runs on a variety of platforms.
R can be downloaded from one of the mirror
sites in http://cran.r- project.org/mirrors.html.
You should pick your nearest locations.

Command line
Execution of commands in R is not menu
driven. (Not like Clicking over buttons to get
outcome). We need to type the commands

Prompt sign(>)
Once R is started, there is a console awaiting
for input. (>) is the prompt sign in R. We can
enter numbers and perform calculations.
>Welcome to this course
R as a calculator
> 2+3
[1] 5
> 2*3
[1] 6
> 2/3
[1] 0.6666667
> 2*5-4/6
[1] 9.333333
Pound sign#

All text after the pound sign "#" within the


same line is considered a comment.
> # Welcome to R
> 3+5 # Addition of numbers
[1] 8
The function c( ) is used to combine numeric
values into a vector
> x=c(1,2,3)
>x
[1] 1 2 3
If we add a and b, the sum would be a vector
whose members are the sum of the
corresponding elements of a and b.
> a=c(1,2,3)
> b=c(4,5,6)
> a+b
[1] 5 7 9
If we multiply a by 6, we get a vector with
each of its members multiplied by 6.
> a*6
[1] 6 12 18
Similarly for subtraction, multiplication and
division, we get new vectors via member wise
operations.
> a=c(1,2,3)
> b=c(4,5,6)
> a-b
[1] -3 -3 -3
> a=c(1,2,3)
> b=c(4,5,6)

> a*b
[1] 4 10 18
> a/b
[1] 0.25 0.40 0.50
If two vectors are of unequal length, the shorter
one will be recycled in order to match the
longer vector
> x=c(1,2,3)
> y=c(4,5,6,7,8)
> x+y
[1] 5 7 9 8 10
Warning message:
In x + y : longer object length is not a multiple
of shorter object length
seq() function generates a sequence of
numbers.
> seq(1:5)
[1] 1 2 3 4 5
Sequence with constant increment
> seq(from=20,to=25,by=2)
[1] 20 22 24
Sequence with constant decrement
> seq(from=25,to=20,by=-2)
[1] 25 23 21
> seq(to=20,length=14)
[1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> seq(from=10,length=13)
[1] 10 11 12 13 14 15 16 17 18 19 20 21 22
rep() function replicates the values in x.
> rep(1,5)
[1] 1 1 1 1 1
> rep(1:3,5)
[1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
abs() function is to find absolute value
> abs(-10)
[1] 10
> abs(c(-2,-6,-8))
[1] 2 6 8

sqrt() function is to find square root


> sqrt(4)
[1] 2
> sqrt(c(2,3,4))
[1] 1.414214 1.732051 2.000000
sum() function is to find the addition of values
> sum(c(1,2,3,4))
[1] 10
prod() function is to find the product
> prod(c(1,2,3))
[1] 6
round() function is used to round the decimal
places
> round(4.56789123,3)
[1] 4.568
length() function gets the length of a vector.
> x=c(1,2,3,4,5,6,20,35,57)
> length(x)
[1] 9
R Matrix

A matrix is a rectangular array with m rows


and n columns. . R has a lot of operator and
functions that make matrix handling very
convenient.
Matrix row and column count:
nrow() function defines the row number of a
matrix
ncol() function defines the column number of
a matrix
data() function assigns specified values to the
matrix elements.
dim() function gives the dimension of a matrix
Create a matrix x of order 3  2 with elements
1,2,3,4,5,6
> x=matrix(nrow=3,ncol=2,data=c(1,2,3,4,5,6))
>x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
Create a matrix x of order 3  2 with elements
1,2,3,4,5,6 where 1,2 as the elements of first
row, 3,4 as the elements of second row and 5,6
as the elements of third row.
> x=matrix(nrow=3,ncol=2,data=c(1,2,3,4,5,6),byrow=T)

>x
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
Find the number of rows, number of columns
and dimension of matrix x.
> x=matrix(nrow=3,ncol=2,data=c(1,2,3,4,5,6),byrow=T)

>x
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
> a=nrow(x)
>a
[1] 3
> b=ncol(x)
>b
[1] 2
> dim(x)
[1] 3 2
We can access a single element of a matrix
with x[i,j] function
Find the (2,2) entry in matrix x
> x=matrix(nrow=3,ncol=2,data=c(1,2,3,4,5,6),byrow=T)

>x
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
> x[2,2] #element of [2,2] in matrix x
[1] 4
Assigning a specified number to all matrix
elements
> y=matrix(nrow=3,ncol=4,data=5)
>y
[,1] [,2] [,3] [,4]
[1,] 5 5 5 5
[2,] 5 5 5 5
[3,] 5 5 5 5
diag() function defines the diagonal matrix
> x=diag(3,nrow=3,ncol=3)
>x
[,1] [,2] [,3]
[1,] 3 0 0
[2,] 0 3 0
[3,] 0 0 3
Create a matrix x=diag(1,2,3)
> x=diag(c(1,2,3),nrow=3,ncol=3)
>x
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 2 0
[3,] 0 0 3
>
t() function returns a transpose matrix
Find the transpose of matrix x.
>x=matrix(nrow=3,ncol=4,data=c(1,2,3,4,5,6,
7,8,9,10,11,12))
>x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> t(x)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
Multiplication of a matrix with a constant
> x=matrix(nrow=2,ncol=3,data=c(1,2,3,4,5,6))
>x
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> 5*x
[,1] [,2] [,3]
[1,] 5 15 25
[2,] 10 20 30

Addition of matrices
> a=matrix(nrow=2,ncol=2,data=3)
>a
[,1] [,2]
[1,] 3 3
[2,] 3 3
> b=matrix(nrow=2,ncol=2,data=c(1,2,3,4),byrow=T)

>b
[,1] [,2]
[1,] 1 2
[2,] 3 4
> a+b
[,1] [,2]
[1,] 4 5
[2,] 6 7
Subtraction of matrices
> a=matrix(nrow=2,ncol=2,data=3)
> b=matrix(nrow=2,ncol=2,data=c(1,2,3,4),byrow=T)

> a-b
[,1] [,2]
[1,] 2 1
[2,] 0 -1
rbind() function add new row to a matrix
cbind()function add new column to a matrix
> a=matrix(c(2,3,3,4),nrow=2,ncol=2)
>a
[,1] [,2]
[1,] 2 3
[2,] 3 4
> a=rbind(a,5:6)
>a
[,1] [,2]
[1,] 2 3
[2,] 3 4
[3,] 5 6
> a=cbind(a,7:9)
>a
[,1] [,2] [,3]
[1,] 2 3 7
[2,] 3 4 8
[3,] 5 6 9
x[i,] function select a particular row from a
matrix
> a=matrix(data=c(2,3,5,3,4,6,7,8,9),nrow=3,ncol=3)
>a
[,1] [,2] [,3]
[1,] 2 3 7
[2,] 3 4 8
[3,] 5 6 9
> a=a[2,]
>a
[1] 3 4 8
x[-i,] function delete one row from a matrix
> a=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4)
>a
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> a=a[-2,]
>a
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 3 6 9 12

x[,-j] function delete one column from a


matrix
>a
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 3 6 9 12
> a=a[,-4]
>a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 3 6 9

solve() function used to solve the


simultaneous equations
Solve 2x+3y=4; 3x+4y=4
> a=matrix(c(2,3,3,4),nrow=2,ncol=2)
> b=matrix(c(4,4),nrow=2,ncol=1)
> solve(a,b)
[,1]
[1,] -4
[2,] 4
Value of x=4, y=-4

You might also like