How to find and name contiguous non-zero entries in a sparse matrix in R?
问题 My problem is conceptually simple. I am looking for a computationally efficient solution of it (my own one I attach at the end). Suppose we have a potentially very large sparse matrix like the one on the left below and want to 'name' every area of contiguous non-zero elements with a separate code (see matrix on the right) 1 1 1 . . . . . 1 1 1 . . . . . 1 1 1 . 1 1 . . 1 1 1 . 4 4 . . 1 1 1 . 1 1 . . 1 1 1 . 4 4 . . . . . . 1 1 . . ---> . . . . 4 4 . . . . 1 1 . . 1 1 . . 3 3 . . 7 7 1 . 1 1