lundi 7 décembre 2020

How can I get my if-statement to return "True"?

I am creating a function that checks if my data is a matrix and if the matrix is square. After that, I am using a for loop to check the off diagonal elements. To do that, I am comparing the transposed matrix against the original matrix. When I pass a matrix that isnt symmetrical through this function, it will return false, but when I return one that is symmetrical, like small below, nothing happens and I do not know what to do to make it return True.

 IsSymmetric <- function(MyMatrix){
          if(is.matrix(MyMatrix) == TRUE & dim(MyMatrix)[1] == dim(MyMatrix)[2]){
            trans <- t(MyMatrix)
            for(i in 1 : dim(MyMatrix)[1]){
              for(j in 1 : dim(MyMatrix)[2]){
                if(trans[i,j] != MyMatrix[i,j]){
                  return("FALSE")
                  break()
                }
              }
            }
          } 
        }
        MyMatrix <- matrix(1:9, nrow = 3, ncol = 3)
        IsSymmetric(MyMatrix)
        small <- matrix(rep(0,10*10), nrow=10)
        IsSymmetric(small)

Aucun commentaire:

Enregistrer un commentaire