ramhiser / sparsediscrim

Sparse and Regularized Discriminant Analysis in R
Other
15 stars 5 forks source link

Write helper function to test if a matrix is diagonal. #10

Closed ramhiser closed 11 years ago

ramhiser commented 11 years ago

Here is an example, where we test if the Frobenius norm of the difference between the matrix consisting of the diagonal elements of x and x itself is less than some tolerance.

is_diagonal <- function(x, tol = 1e-8) {
  norm(diag(diag(x)) - x, "F") < tol
}