Please solve the following using Rstudio
Construct a function called mat1 that inputs a matrix and
outputs the following as a list
a. Sum of the elements of the matrix
b. Product of the matrix and its transpose (use the t
function)
c. Inverse of the matrix in (b.) (use the solve
function)
And construct another function called mat2 that does the same as mat1 except that it first checks if the input is a matrix and outputs an error message (and nothing else) if it is not a matrix.
#r-code is given below:
mat1=function(m1)
{
s=sum(m1) #sum of
elements of the matrix
p=m1%*%t(m1) #sum of the product of the
matrix and its transpose
i=solve(m1) #inverse of
the matrix
list(s,p,i)
}
mat2=function(m2)
{
if(is.matrix(m2)==TRUE)
{
print("Input is a matrix")
s=sum(m2)
#sum of elements of the matrix
p=m2%*%t(m2) #sum of
the product of the matrix and its transpose
i=solve(m2)
#inverse of the matrix
list(s,p,i)
}
else print("ERROR: Input is not a matrix")
}
Get Answers For Free
Most questions answered within 1 hours.