1
當我試圖在相同的正則和稀疏矩陣上應用log
函數時,獲得的結果是不同的。在應用這些功能時,我應該記住什麼?以下是一個可重複的例子。當對數函數應用於相同的正則矩陣和稀疏矩陣時,爲什麼結果會不同?
TestMatrix = matrix(c(3,1,0,0,0,4,0,1,0,0,2,1,1,2,0,6,1,0,1,0,1,0,0,0,0),5,byrow = TRUE)
TestSparseMatrix = Matrix(TestMatrix,sparse = TRUE)
# Logarithmic function when applied to regular matrix
-log(TestMatrix/rowSums(TestMatrix), 2)
#Output
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0.4150375 2.000000 Inf Inf Inf
#[2,] 0.3219281 Inf 2.321928 Inf Inf
#[3,] 1.5849625 2.584963 2.584963 1.584963 Inf
#[4,] 0.4150375 3.000000 Inf 3.000000 Inf
#[5,] 0.0000000 Inf Inf Inf Inf
# Logarithmic function when applied to Sparse matrix
-log(TestSparseMatrix/rowSums(TestSparseMatrix), 2)
# Output
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0.2876821 1.386294 Inf Inf Inf
#[2,] 0.2231436 Inf 1.609438 Inf Inf
#[3,] 1.0986123 1.791759 1.791759 1.098612 Inf
#[4,] 0.2876821 2.079442 Inf 2.079442 Inf
#[5,] 0.0000000 Inf Inf Inf Inf