Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- neighbours <- array(0, c(100,100))
- for (i in 1:100) { neighbours[i,i] = 1 } #reflexive
- > class(neighbours[5])
- [1] "numeric"
- > class(neighbours[5]) <- "integer"
- > class(neighbours[5])
- [1] "numeric"
- neighbors <- array(FALSE, c(100,100))
- diag(neighbors) <- TRUE
- > object.size(array(0, c(100,100)))
- 80200 bytes
- > object.size(array(FALSE, c(100,100)))
- 40200 bytes
- m <- matrix(0L, 100, 100)
- diag(m) <- 1L
- m2 <- diag(1L, 100, 100)
- > object.size(m)
- 40200 bytes
- > object.size(m2)
- 80200 bytes
- R> neighbours <- array(0, c(100,100))
- R> for (i in 1:100) { neighbours[i,i] = 1 }
- R> str(neighbours)
- num [1:100, 1:100] 1 0 0 0 0 0 0 0 0 0 ...
- R> storage.mode(neighbours) <- "integer"
- R> str(neighbours)
- int [1:100, 1:100] 1 0 0 0 0 0 0 0 0 0 ...
- R> storage.mode(neighbours) <- "logical"
- R> str(neighbours)
- logi [1:100, 1:100] TRUE FALSE FALSE FALSE FALSE FALSE ...
- library(Matrix)
- Matrix(diag(1,4) , sparse=TRUE)
- #---------
- 4 x 4 sparse Matrix of class "dsCMatrix"
- [1,] 1 . . .
- [2,] . 1 . .
- [3,] . . 1 .
- [4,] . . . 1
- > m <- matrix(rnorm(25), 5)
- > m[] <- as.integer(m)
- # you do need those square-brackets or the structure becomes a dimensionless vector.
- > m
- [,1] [,2] [,3] [,4] [,5]
- [1,] 0 0 -1 0 0
- [2,] 1 0 0 0 0
- [3,] 1 0 0 0 0
- [4,] 0 0 0 0 0
- [5,] 0 0 0 -1 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement