2015-12-21 189 views
1

我想多使用基本功能%*%兩個MATRIX在R,例如矩陣乘法錯誤中的R

e <- dget(file = textConnection(
    'structure(list(u = c(4.00976069489207, 178.604060187876, 118.918710690719, 
-229.205481965294, -1.24006047251441, -95.9342713118203, 47.8278332993814, 
55.0912976611644, 4.80132026139472, -32.5090533739312, 5.42000139109041, 
512.212023076139, -1757.6162157742, 8.66354612432497, -0.379352538651801, 
111.504887168796, -266.331168788704, 2000.23693211855, -0.242980714505393, 
0.955710131117485, -5.22385427197696, -11.7346154789902, 17.6649283453744, 
9.21910124550959, -4.69614458304386), v = c(-0.0441225117765498, 
-447.707663146649, -424.574055570449, -6.39547057226855, -20.7072050928182, 
89.8052473454295, -45.1398725064628, -206.218711680996, 20.5204241988995, 
-83.8411936690584, 4.82678516352075, 16.0132244355639, -54.5879902077763, 
4.55541729541892, -0.417357418730262, 4.27524015188328, -10.6298829582797, 
89.9130220212751, 4.03963376820411, -3.90611247380684, -0.466636964249346, 
-0.583851818052153, 0.823044295174896, 0.921420474586739, -0.461728372400557 
)), .Names = c("u", "v"), row.names = c(NA, -25L), class = "data.frame")' 
)) 
norms <- apply(e, 1, function(x) sqrt(x %*% x)) # Edge lengths 
v <- diag(1/norms) %*% e 

我沒有看到我的數據和代碼的任何奇怪的事情,卻得到一個錯誤信息:

Error in diag(1/norms) %*% e : 
    requires numeric/complex matrix/vector arguments 

感謝您爲解決此問題提出的任何建議。

+0

您的示例不完整。什麼是a2 $船體? – Heroka

回答

3

edata.frame,參見str(e)。你需要矩陣乘法的矩陣,這就是錯誤告訴你的。

v <- diag(1/norms) %*% as.matrix(e) 
+0

謝謝。需要仔細檢查輸入數據的類 – Bangyou