3
除了通常的矩陣乘法,我需要一個元素操作。以下工作正常:矩陣和向量的元素乘積
# this works
Bmat <- structure(c(3L, 3L, 10L, 3L, 4L, 10L, 5L, 8L, 8L, 8L, 3L, 8L, 8L, 2L, 6L, 10L, 2L, 8L, 3L, 9L), .Dim = c(10L, 2L))
yvec <- c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
Bmat * yvec
# [,1] [,2]
# [1,] 6 6
# [2,] 6 16
# [3,] 20 16
# [4,] 6 4
# [5,] 8 12
# [6,] 20 20
# [7,] 10 4
# [8,] 16 16
# [9,] 16 6
# [10,] 16 18
然而,下面的失敗:
# this fails
Amat <- structure(c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(10L, 2L))
xvec <- structure(c(1.83475682418716, 1.48154883122634, 1, 1, 1, 1, 1, 1, 1, 1), .Dim = c(10L, 1L))
Amat * xvec
#Fehler in Amat * xvec : nicht passende Arrays
這是爲什麼?它與Bmat
是一個整數矩陣有什麼關係?我如何使第二個代碼工作?
'艾買提* as.numeric(XVEC)' –
@(d.b,大衛的Heckmann):你的兩個解決方案的工作,謝謝! –