1. Write down a function in R which can compute the product of two matrices, wit
ID: 3048986 • Letter: 1
Question
1. Write down a function in R which can compute the product of two matrices, without using any in-built R-functions. Then use that function to compute the product of the following matrices. (10 points) 1 1 1-1 1 458 9 2-6 -9-4 0 1 25-5 4 1 2 4 3 49 8 2. Write down a function in R which can be used to compute the combination of r events out of n, i.e., (r), without using any in-built R functions. Then use the function to find the value of (10 points) 50 3. Suppose the random variable X has pdf 0 if xExplanation / Answer
Will answer first question only
Here is the code for the function:
prod_mat = function (A,B){
if (ncol(A) != nrow(B)) stop ("Incompatible Matrices")
C = matrix(0, nrow = nrow (A), ncol = ncol(B))
for (i in 1:nrow (A)){
for (j in 1:ncol(B))
C[i,j] = sum(A[i,] * B[,j])
}
return (C)
}
A = matrix (c(1,-2,3,2,-1,4,3,-5,9,4,0,8), nrow = 3)
B = matrix(c(1,1,2,-1,-1,4,-6,2,1,5,-9,5,1,8,-4,-5,-1,9,0,4),nrow = 4)
prod_mat(A,B)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.