Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying if only the diagonal is non-zero

Tags:

r

Let's say I have a matrix with all 0's with the exception of the diagonal.

m <- matrix(ncol=3,nrow=3)
m[,1] <- c(1,0,0)
m[,2] <- c(0,1,0)
m[,3] <- c(0,0,1)

What logical check could I use to test if the upper and lower triangle of the matrix are zero?

like image 921
Brandon Bertelsen Avatar asked Dec 15 '22 22:12

Brandon Bertelsen


1 Answers

all(m[lower.tri(m)] == 0, m[upper.tri(m)] == 0)
like image 153
Grega Kešpret Avatar answered Jan 01 '23 01:01

Grega Kešpret