require(SFSI)
# Simulate matrix
X = matrix(rnorm(5000),ncol=5)
head(X)
# Save matrix as double-precision
saveBinary(X,paste0(tempdir(),"/Matrix1.bin"),precision.format="double")
# Save matrix as single-precision
saveBinary(X,paste0(tempdir(),"/Matrix2.bin"),precision.format="single")
# Read the double-precision matrix
X2 = readBinary(paste0(tempdir(),"/Matrix1.bin"))
head(X2)
max(abs(X-X2)) # No loss of precision
object.size(X2) # Size of the object
# Read the single-precision matrix
X3 = readBinary(paste0(tempdir(),"/Matrix2.bin"))
head(X3)
max(abs(X-X3)) # Loss of precision
object.size(X3) # But smaller-size object
# Read specific rows and columns
index.row = c(2,4,5,8,10)
index.col = c(1,2,5)
X2 = readBinary(paste0(tempdir(),"/Matrix1.bin"),index.row=index.row,index.col=index.col)
X2
# Equal to:
X[index.row,index.col]
Run the code above in your browser using DataLab