Values of matrix

Hello guys
my questions is if there is any function in R that can check if all elements of matrix are zeros , i mean for example is it possible to do it with if statement ,if the elements are zeros return true otherwise return false
thanks a lot

Are you looking for something like this ?

a <- matrix(rep(0,4), nrow = 2)
a
#>      [,1] [,2]
#> [1,]    0    0
#> [2,]    0    0
b <- a
b[1, 1] <- 1
b
#>      [,1] [,2]
#> [1,]    1    0
#> [2,]    0    0
all(a == 0)
#> [1] TRUE
all(b == 0)
#> [1] FALSE

Created on 2018-09-11 by the reprex package (v0.2.0).

It is just testing assertions then test if all TRUE or not.

2 Likes