Lecture 07: Matrix Operations

IIT Kanpur July 201
20 followers
3 Video Views·Jan 26, 2024
[Music]
Hello friends welcome to the course
multivariate procedures with r you can
recall that in the last couple of
lectures we were talking about different
types of calculations in the our
software
and you have understood that the way R
does different type of mathematical
procedures like as addition subtraction
division
multiplication that is possible with the
scalar as well as data vector and this
operations are little bit different than
other
softwares but I'm sure that now you have
understood those operations so now in
this lecture and in the next lecture we
are going to talk about the Matrix
Theory
the first question comes here why should
we learn this Matrix Theory you see what
is the basic objective of this course
this is to have the multivariate
procedures multivariate means there are
more than one
variables so whenever we are trying to
deal with multivariate procedures be it
multivariate normal
distribution be it multiple linear
regression analysis is be it a
discriminant analysis principal
component analysis or
anything the data
is available on more than one
variables what do you mean by
variables Suppose there are two
variables age and weight so there will
be one person we try to record her or
his age as as well as weight now the
second person comes and we try to record
uh the weight and then age Etc and so
on and similarly you can think that
there are more than one variables or
more than two variables which are going
to affect the process so every set of
observation will have not only one value
but more than one values suppose if you
want to record the data on the health of
say
20% and now you define that health is
going to be measured by say age weight
height blood pressure blood sugar level
Etc then each of the 20 persons will
have observations on all these
variables so the question comes here how
are you going to handle such data sets
the first question
comes we have to give the input to a
software and in most of the cases you
will see that such
observations are arranged in the form of
vectors and
matrices uh remember I'm not talking of
the data Vector I'm talking of the
vectors in The Matrix Theory like as row
vector or column
vectors so it is important for us to
understand that how R is handling the
Matrix and its
operations the before going forward I
assume that you all are aware about the
Matrix Theory and its various
operations and you will agree with me
that uh many of the operations in The
Matrix Theory they are very much
different than the usual escalar
operations and when we try to apply
those procedures in the r
software one aspect ECT which you always
have to keep in mind is that R is going
to follow the same mathematical rules
for the Matrix
theory in case if you make a mistake in
giving the input to the data or
somewhere else are may not be able to
exactly diagnose what is the mistake
what is the
problem but this is only you have to uh
see and uh find out what is the problem
for example
in case if you are trying to multiply
two
matrices so whenever you are trying to
multiply two matrices the rule is that
if there are two matrices say A and B
and suppose one's order is M byn then
the order of the second Matrix has to be
n by P or something like
this so such type of
operations I am assuming that you know
so in this uh lecture I am going to
introduce this uh matric Theory to brush
up your knowledge so that you are aware
and then you have a quick review of the
basic concepts in The Matrix Theory
although there are many many operations
in The Matrix Theory and uh it is
practically impossible for me to cover
all those operation but I will try to
take up some standard operations in this
lecture and in the next lecture and
hopefully after that uh you will be
confident that if you want to use any
other Matrix procedure you can use it
and and you can find out the way or the
syntax or the command in the our
software so with this objective let us
begin our lecture and try to have some
glimpse of the Matrix operations in our
software right so in this lecture first
we try to Define define that what are
matrices and as I said that matrices are
important object in any calculation and
particularly you are trying to do the
multivariate analysis then this Matrix
Theory becomes an Inseparable part of
the whole analysis what is a Matrix
Matrix is the arrangement of elements in
rows and columns for example if I say in
mathematics we try to write down a
matrix like this and then I will say
here this is the first element a11 this
is the a second element A1 two and
similarly in the next element a21 a22
and so on so these are called
rows and these are called
columns right and this subscript if you
try to see what is this
a11 this is indicating the number row
number and column
number similarly if I say here
a12 so a12 is indicating that is
the element in
the first row and second
column right and if there are such
here one 2 up to here P rows and suppose
here one two up to here Q
columns or say n
columns then this is called as a matrix
of
order P by
n right so this Matrix is a rectangular
array with P rows and and
columns and as we write in the book as I
explained you suppose if I say if I try
to denote the matrix by here x then
usually by xig J we write down its
elements but this xig J is written in
the book that is how we try to
understand the Matrix Theory from the
book
but in the software and in the
programming language usually we try to
write this thing as X inside the
brackets I comma J so here I goes from
here 1 to P and J goes from here 1 to n
right now when we are trying to deal
with the Matrix operation in any
software or in any programming language
like as R then the element of this
Matrix can be an object for example that
can be a number that can be a string
and but in mathematics when we are
trying to do the mathematical operation
then usually we are interested in the
numerical Matrix numerical metrix means
all the elements in the metric should be
some numbers they should not be
character otherwise we cannot do the
mathematical operations although in
order to do the data analysis many times
we try to arrange the characters also in
the format of a matrix but anyway at the
moment we are not going to consider uh
those things and we are going to assume
that the elements in my Matrix they are
the real numbers right so now uh I have
two options here that first I try to
give you the basic command of the Matrix
Theory and then I try to explain you how
you can do it in the r software or let
me take here some
examples and through those examples I
will try to explain you that how you can
make different operations in The Matrix
Theory in this art software so let me
try to take here first example suppose I
want to create a
matrix creating a matrix mean the data
has to be arranged in The Matrix now
when we are trying to create a matrix
what do we need the first ingredient is
that we want to know what is the number
of rows then we want to know what is the
number of columns and then we want to
know what is the data that means what
are the entries in The Matrix so suppose
if I want to create a 4x2 Matrix that
means four
rows and two
columns so if you try to see if there
are four rows and two columns then how
many data entries you require this will
be 4 into 2 that is 8 so now I have to
supply this information to the r
software so in order to create a metrix
or to inform from the r software or to
request the r software to create a
matrix the command is Matrix M A
Rix and then inside the
parenthesis you have to give different
type of options or
information so I want to inform the r
software that there are going to be four
rows so this is optained by writing n
row which is indicating the number of
rows
and I write here number of rows that is
n row equal to four and similarly here n
call n call means here number of
columns so n call is equal to two so
that is indicating that there are four
rows and two columns now I want to
inform our that what is
the numbers or what are the real numbers
which are going to take place inside the
Matrix so I try to give here eight
values which I have combine here in a
data Vector so and I give it and I use
here this option data so data will
communicate to the r software that these
are the values which are going to be
inside the
Matrix so once you execute this thing
you will get here this type of outcome X
will look like
this see here the if you try to see the
data I have given here for the sake of
understanding in uh an increasing value
1 2 3 4 5 6 7 8 now if you try to see
that how this data is going to be
entered in
the Matrix X it is here one comes here
first then comes here two then three
then four and from here it goes to the
next column five then six then 7 then
8 so if you try to see the data here is
arranged column wise
and the
same operation when I try to do in the r
software comes out to be here like this
that you can verify this is the same
output which is
here and what you can observe here I
will explain you later that what are
these indicating for example this here
one this here one and if you try to
observe how it is
written this is written like first uh
You observe these
brackets then it is here the number one
and then here
comma whereas in the column case please
try to concentrate where I am writing it
is here bracket then there is blank
space comma and then it is here one
right so if you try to see this is
indicating First
Column and similarly this is indicating
second
column
similarly this is
indicating first
row and this is indicating second row
and so on right and in case if you want
to
write what is the address of this number
here two so this you can see here this
is on
the second row and First Column so this
is this is how we try to give the or the
r is trying to give the addresses to
different locations to rows columns as
well as individual number so this is I
just want to add your attention so that
later on when I I'm trying to explain
you that how to access any particular
row column or element this is going to
be helpful right so now you can see here
that this here this parameter n row that
is defining the number of rows in a
metrix n call is trying to define the
column numbers of the Matrix and data is
assigning the specified value to The
Matrix right and when you are not giving
any option then this Matrix is trying to
assign the observation column wise you
can see here and if there are more
columns it will go like this right so
this is what you have to keep in mind
right
so now in case if you want to access any
particular
element of this uh Matrix because it may
be required in various multivariate
procedures that you want to do something
on a particular element and then you
need to write a command so that a
particular element can be
obtained so as I explained you uh just
now uh suppose you want
to extract
this number seven so now if you try to
see here this number seven is lying on
the third row and in the second column
so what you have to write here the name
of the Matrix X and using the square
brackets you have to write 3 comma
2 and in case if you try to write down
here x inside the square bracket 3 comma
2 you will get here the value here 7
right uh one thing I can explain you
that as in mathematics we have different
types of bracket like as parenthesis
curly brackets and square brackets so in
R when we are trying to do the
mathematical operation then we use only
the parenthesis like
this the curly brackets and square
brackets they are used for some other
assignments for example they can have
different role for example here you can
see when we are trying to find a
particular value in The Matrix then we
are trying to use this square
brackets all right so now in case if my
objective is that I want to arrange the
same
data rowwise
vwise as I explained you here that if
you don't give here any option then this
data is going to be arranged columnwise
now I want to arrange it rowwise so how
to do it so I will use the same command
here same data set but I want to arrange
in the rowwise so so I am using the
command here Matrix n row and call and
data but now I add here one more
parameter by
row by y r o
w right and I write here B is equal to
True T Ru in all in uppercase alphabets
actually here this
true and false
false they are The Logical
operators and instead of true and false
you can also use here say t or F also
the first
alphabet so if you try to understand
what I'm trying to inform to the r
software I'm saying by is equal to
true that means I am asking r that when
you try to arrange the data then you
have to arrange the data by rows or and
this statement is
true that means you please arrange the
data row-wise in case if I write down
here false by row is equal to false that
means I'm trying to inform the r
software that you need not to arrange
the data rowwise that means obviously
the opposite will be correct that the r
has to the data column
wise so if you try to see here by using
this new parameter by row equal to True
how this data is going to be obtained so
you can see here this Matrix I am
calling here as a y and the Y is coming
out to be like this you can see here now
the same data is here 1 2 two it is
going first in the Row first row then it
come to the second row 3 to 4 then 5 to
6 then 7
to8 so this is how we try to control
this data uh data Arrangement and you
can see here that in this uh screenshot
I can show you that here I am not using
here by byro option where whereas in the
second case I'm trying to use here the
brro equal to true and the data is here
the same 1 to8 numbers number of rows
and number of column colums are also the
same but here the data is arranged
column
wise but in the second case the data is
arranged rowwise
right so let me try to show you first
these examples on the our software so
that you can be confident about them so
let me try to just uh copy this thing so
and
uh then I try to copy this
thing
right so even if if you want to write it
in the same line that also can be done
that is the same thing all right now if
you try to see what is here x this is
the same operation which I just
explained you right now in case if I try
to change this command and I try to
write down here by row is equal to
true so okay I will just write down here
as a t only so that you can be confident
that t and capital t r u e both are
working and let me give this Matrix a
name y so you can see here Y is now here
like
this so that is the same operation which
I just uh explained you right and
suppose if I want to extract any
particular element of here x suppose I
want to extract here X3 comma 2 so so
this is going to be here 7 whereas if I
try to arrange or retrieve the same
element in the Y Matrix then it is going
to be here six right and that you can
see here this is the element in the X
Matrix and this is the element in the Y
Matrix right so now you you can be
confident that yeah this is working
without any problem and uh if you try to
uh understand here one
concept that uh try to look at this
screenshot and uh in this screenshot I
am not writing anything about by Row in
the first uh here say by
row so what is the value of your by
row so obviously by row here is actually
false but this is the default
option that even if you don't write
anything about by
row then R is going to assume it to be
false that is the default option so you
have to be careful according to your
requirement and need you need to use
this
operation
right okay so now we move
forward now I would like to introduce
you that uh sometimes the data is coming
from external sources and it is arranged
in some
Metric so in this example what you have
done here here you have tried to create
a matrix of order
4x2 but suppose some unknown data set is
coming from
outside and you don't know what is the
number of rows what is the number of
columns Etc and in order to move further
you need to have such type of
information so the question is that how
would you know what are the dimension of
the metrix what are the number of rows
and what are the the number of columns
so if you want to find out the dimension
of the Matrix then the command here is
diim dim which is the short form of
Dimension if you simply write dim and
inside the parenthesis X the name of the
Matrix you will get here 4x2 which is 4
comma 2 so you have to observe how it is
written right and if you want to know
the number of rules in The Matrix X then
you have to use the command n row inside
the parenthesis X right and uh it will
give you the option here the answer here
here as a four and similarly if you want
to know the number of columns then you
have to use the command and call X and
it will give you the number here to
right so this is how you can have
different type of
information and uh I would like to
inform you that you see I'm trying to uh
consider here only very limited number
of options which are more popular or
they are going to be used in our course
particularly but but there are many more
options so I would recommend you that
you please try to look into the help and
you simply say help inside the
parenthesis within double quotes Matrix
and you will get here all the details
about the Matrix and there are many
other options which are mean avable like
as Matrix is Matrix suppose that means
if if there is some unknown data set and
you want to know whether it can be
treated as a matrix or not then this s
do Matrix is going to help you
but definitely I'm not going into these
details and uh but I would certainly
like to show you the use of this uh dim
command and nro N X right so let me try
to clear the screen and let me try to
have this metrix here x so you can see
here this is here like this and now if I
try to write down here DM of X which is
here like this 42 that means there are
four number of rows and two columns and
if I say here end row X so so this is
here four and if I say here n call X
this is here two and similarly if you
try to mean take any other Matrix then
the similar observations can be can be
obtained for example if I try to show
you that if you try to make any mistake
then what will happen suppose if I am in
the Y Matrix suppose if I try to add
here say two more numbers 9 and 10 so
there are to Total 10 values but you
want to arrange them in the four rows
and two columns which is wrong but it
will try to say that it is y is giving
here like this but it is giving you a
warning message and if you try to see
what it is trying to do it is trying to
take only this eight
values and now similarly if you try to
make another mistake that if you try to
make here the number of rows to be here
six that means you need here 12
observation but now you have given only
here 10 observation then what happens it
is again trying to give you the one war
that there is something is wrong you try
to find out and if you try to see here
how it is doing so it is here 1 2 8 it
is arrang say here 10 it is arranging in
the five rows and two columns but after
that it is
repeating so this is you have to keep in
mind that this is how the r is working
so if you don't give the proper input
and if you ignore the warning messages
in your simulation or in your
computation then the r is going to use
this value but you wanted to use only up
to here 10 right so this is what you
have to be careful so let me try to
clear the screen and come back to our
slides right so now I try to give you
here different types of examples in
which we are going to give you the idea
that how different types of matrices can
be can be defined well these examples
will help you when you are working in
the multivariate procedure and if you
want to input a particular type of data
or if you want to do a particular type
of operation suppose you want to have a
matrix in which all the element should
be the
same right suppose I want to create a
4x2 Matrix in which all the element
should only be five so I can use here
the same command Matrix then n row equal
to 4 n call equal to 2 but I will say
only data equal to here five and as soon
as you try to do it here you will get
here this type of observation this type
of Matrix where you can see there all
the eight elements in The Matrix X they
are the same F so this type of
operations are many times needed right
and uh suppose if you want to create a
diagonal matrix for example identity
Matrix is a diagonal matrix in which all
the elements on the diagonal elements
are one and of diagonal elements are
zero what are the diagonal of diagonal
elements if you try to take a square
Matrix then uh if you try to write down
here elements like a12 and so on A2 1
a21 a22 Etc like this then this is
actually here the diagonal so these are
called
the diagonal
elements and all these elements which
are not on the diagonal they are called
as of
diagonal elements
and this part is called as upper
diagonal and the elements in the lower
part they are called
as lower
diagonal right so if you want to create
a diagonal matrix suppose you want to
create a identity Matrix of order 2x two
then the command here is e i
a and you see here one which is the
value of data
and uh you define the number of rows and
number of columns as n row equal to two
and N call equal to two and if you try
to see here this outcome will come here
like this right and if you want to have
any other value on the diagonal then
also you can play with this value one
and I will try to show you it on the r
console right and uh uh there is another
Concept in The Matrix Theory which is
transpose of a matrix transpose of a
matrix is defined s when you try to
Interchange the rows and columns suppose
if I say here my Matrix here is 1 2 3 4
and now I try to Define here the
transpose which is dened by X Prime
which is here x transpose or then the
first row will become here First Column
and the second row will become here
second column so this is a very popular
Operation and if you want to do it then
the command here is TX but let me try to
show you with the an example suppose I
try to Define here the uh the same
Matrix X which is 4x2 Matrix and the
data is here 1 to8 right this is another
way of writing the elements 1 2 3 4 up
to 8 like this right and by row is equal
to true so you get here this Matrix
right 1 2 3 4 5 6 7 8 like this in which
the data is arranged rwise now we want
to find out the transpose of this Matrix
the r command to find out the transpose
of a matrix is T inside the parenthesis
you have to write down the Matrix so I
write down here T inside the parenthesis
X and then you can see here the
outcome and suppose I stored this
transpose in a and Matrix XT so this is
the here the outcome that now this one
and two they are coming on the First
Column three and four they are coming on
the second column five and six they are
coming coming on the third column and
seven and eight they are coming on the
fourth column and if you try to is
compare them in this screenshot you can
see here your X Matrix was like this one
two were the first row but now one two
are becoming the First Column the second
row is becoming here the second column
third row is becoming the third column
fourth row is becoming the fourth column
right so this is how we try to do but
before going forward let me try to
uh show you these operations on the r
console so that you feel more
comfortable suppose I want to create
here a matrix 4X two in which all the
elements are going to be here five so
you can see here five and similarly if
you want to change this number suppose I
want to give this number to 15 so you
can see here x is coming out to here 15
right and uh similarly if you want to
create here a diagonal matrix
so you can see
here this is here your diagonal matrix
of order 2x2 which is an identity Matrix
and if you want to change the order of
this Matrix that means if you want to h
a 3X3 identity Matrix now you can see
here there are three elements and if you
try to change the value on the diagonal
suppose if I want to make it here two so
you can see here now this is a diagonal
matrix uh of in which all the diagonal
elements are two and on the other hand I
can also show you that if you want to
make diagonal matrix of different
numbers so I can see you here see uh 1
comma 2 comma 3 and you will see here
this will come out to be here like this
that the first diagonal has value one
second diagonal two and third diagonal
here three right right and uh before I
try to give you other thing let me first
try to show you the transpose concept
and then I will try to show you
something where you have to be careful
so this is your here x Matrix and if you
want to find out your transpose of X
this is coming here like this
right okay so now if you try to see
suppose if I try to write down this
command here which I used
earlier
right right if you try to recall this
thing X this is your here x now suppose
if I don't write this parameters here
and suppose if I write down here four I
delete n row I delete the word n call
and I delete the word data and if I try
to execute it it will give me the same
value no it is not happening because the
first value it has taken which is here
the data
right so you have to be very careful
that you should not go for a shortcut
but I always recommend that you try to
write down this n row end call all these
values very carefully and if you for
example if you try to see here if I try
to write down here data equal to this
then what happens you can see here it is
giving you the same metrix but in this
case what is happening it is trying to
take here the first value four as the
number of rows and second value as a
number of call number of columns and if
I try to suppose change
it you can see here this becomes here
like
this but as a lay man it is very
difficult for me to understand by
looking as 4A 2 and 2A 4 that uh whether
it is the number of rows first then the
number of column or vice versa so my
simple suggestion to you all is that
always write the parameter name so that
it is 100% clear to everyone what we are
trying to do and there is absolutely no
confusion okay so now uh we come to an
end to this uh lecture and you can see
here uh I have taken very simple topics
and my objective was to introduce you
and make you familiar with the metrics
in our software that how are you going
to handle it and you can see here it is
not difficult but as I shown you in the
last uh example that you have to Be
watchful you have to be careful when you
are trying to define the values in the
metrix options uh for example the values
of different parameters you have to
Define it
carefully and please write write all the
commands always in detail so that after
some time if you are looking at the same
command at least you can recall
them beside this thing it is very
important R will do all Matrix operation
following the rules of Matrix and this
is only you who has to give the input in
the right format so this is important
for you to understand the basic
functionality of the Matrix Theory basic
mathematical rules in the math uh in The
Matrix Theory and then try to write down
the syntax and commands
accordingly so I would request you all
that you try to take some simple example
and try to practice that how you can
Define different types of matrices try
to play with the number of row number of
columns diagonal matrix Etc and try to
see that how you can do it and make
yourself familiar with the Matrix Theory
and in the next lecture I will try to
take some more basic operations in The
Matrix Theory like as addition
subtraction inverse Etc so I would
request you that you also try to revise
those concept from your book on The
Matrix theory that how you use to obtain
them so you try to practice it and I
will see you in the next lecture till
then
[Music]
goodbye
[Music]
[Music]
[Music]
