#' Solution of Question 8 in Midterm exam
#' @export
#' @param X matrix variable
ft8 <- function(X) {
n <- nrow(X)
p <- ncol(X)
Q <- matrix(0, n, p)
R <- matrix(0, p, p)
#initial
R[1,1] <- sqrt(sum(X[,1]^2))
Q[,1] <- X[,1]/R[1,1]
#update
for (i in 2:p) {
for (j in 1:(i-1)) {
R[j,i] <- crossprod(Q[,j],X[,i])
X[,i] <- X[,i] - R[j,i]*Q[,j]
}
R[i,i] <- sqrt(sum(X[,i]^2))
Q[,i] <- X[,i]/R[i,i]
}
obj <- list(Q=Q, R=R)
return(obj)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.