Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. neighbours <- array(0, c(100,100))
  2. for (i in 1:100) { neighbours[i,i] = 1 } #reflexive
  3.  
  4. > class(neighbours[5])
  5. [1] "numeric"
  6. > class(neighbours[5]) <- "integer"
  7. > class(neighbours[5])
  8. [1] "numeric"
  9.  
  10. neighbors <- array(FALSE, c(100,100))
  11. diag(neighbors) <- TRUE
  12.  
  13. > object.size(array(0, c(100,100)))
  14. 80200 bytes
  15. > object.size(array(FALSE, c(100,100)))
  16. 40200 bytes
  17.  
  18. m <- matrix(0L, 100, 100)
  19. diag(m) <- 1L
  20.  
  21. m2 <- diag(1L, 100, 100)
  22.  
  23. > object.size(m)
  24. 40200 bytes
  25. > object.size(m2)
  26. 80200 bytes
  27.  
  28. R> neighbours <- array(0, c(100,100))
  29. R> for (i in 1:100) { neighbours[i,i] = 1 }
  30. R> str(neighbours)
  31. num [1:100, 1:100] 1 0 0 0 0 0 0 0 0 0 ...
  32. R> storage.mode(neighbours) <- "integer"
  33. R> str(neighbours)
  34. int [1:100, 1:100] 1 0 0 0 0 0 0 0 0 0 ...
  35. R> storage.mode(neighbours) <- "logical"
  36. R> str(neighbours)
  37. logi [1:100, 1:100] TRUE FALSE FALSE FALSE FALSE FALSE ...
  38.  
  39. library(Matrix)
  40. Matrix(diag(1,4) , sparse=TRUE)
  41. #---------
  42. 4 x 4 sparse Matrix of class "dsCMatrix"
  43.  
  44. [1,] 1 . . .
  45. [2,] . 1 . .
  46. [3,] . . 1 .
  47. [4,] . . . 1
  48.  
  49. > m <- matrix(rnorm(25), 5)
  50. > m[] <- as.integer(m)
  51. # you do need those square-brackets or the structure becomes a dimensionless vector.
  52. > m
  53. [,1] [,2] [,3] [,4] [,5]
  54. [1,] 0 0 -1 0 0
  55. [2,] 1 0 0 0 0
  56. [3,] 1 0 0 0 0
  57. [4,] 0 0 0 0 0
  58. [5,] 0 0 0 -1 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement