Multiply matrices in list

∥☆過路亽.° 提交于 2019-12-01 10:42:23

So generically, how do you multiply corresponding members of two lists? Suppose the matrices with values are in list1 and those with 0/1 are in list2, then one way to do it is with lapply

 answer <- lapply(seq_along(list1),FUN=function(i) list1[[i]]*list2[[i]])

Your resulting matrices will be the elements of answer.

Map is your friend for these sorts of operations. First, let's make some example lists using your data:

l1 <- replicate(3,mat1,simplify=FALSE)
l2 <- replicate(3,mat2,simplify=FALSE)

Now, it's as simple as:

Map("*",l1,l2)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!