-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake.sparseDJB.R
More file actions
40 lines (35 loc) · 1009 Bytes
/
make.sparseDJB.R
File metadata and controls
40 lines (35 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright (C) 2010 Jelmer Ypma. All Rights Reserved.
# This code is published under the Eclipse Public License.
#
# File: make.sparse.R
# Author: Jelmer Ypma
# Date: 18 April 2010
# Revised by Don Boyd May 18, 2015. This version is MUCH faster than the original, which is commented out below
#
# Input: matrix with logical elements
# Output: list with as elements a vector of indices denoting non-zero (TRUE) elements of the matrix
make.sparse <- function(A) {
f <- function(x) which(x!=0, arr.ind=TRUE)
rownames(A) <- NULL # just to be safe
S <- apply(A, 1, f)
S <- as.list(S)
return(S)
}
# make.sparse <- function( A ) {
#
# # start with empty list to append to
# S <- list()
#
# # loop over matrix by row
# for ( i in 1:nrow(A) ) {
# indices <- c()
# for ( j in 1:ncol(A) ) {
# if (A[i,j]) {
# indices <- c( indices, j )
# }
# }
# S <- c( S, list(indices) )
# }
#
# return( S )
# }