![](/img/trans.png)
[英]How can I filter a list within a for-loop in R so my script to move files from one folder to another will function?
[英]R: How can I fix my for-loop so it multiplies every element of a row with the corresponding element of a column?
我有與基質的β nrow=10
和ncol=10
和具有第二矩陣數據nrow=10
和ncol=10
。 我想將beta中的列乘以ever元素中數據的行。
我已經嘗試編寫一個for循環:
solution1 <- matrix(NA,10,10)
for(i in 1:nrow(data)){solution <- matrix(beta*data[i,])}
但是這個突出顯示了10次“不適用”的列表
我不知道我在做什么錯。 其實,我期待着與矩陣nrow=10
和ncol=10
。 也許有人有主意可以提供幫助?
將矩陣的一行的每個元素與另一個矩陣的一列的相應元素相乘的操作與線性代數中定義的矩陣相乘相同。
R具有矩陣乘法運算符%*%
solution <- beta %*% data
閱讀您的說明后,我發現了以下簡單的解決方案。 將所有值逐元素相乘,然后對矩陣進行轉置。
solution <- t(beta*data)
輸出以下結果
[,1] [,2] [,3]
[1,] 1 4 9
[2,] 16 25 36
[3,] 49 64 81
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.