运行图减小了矩阵的维数 [英] Running Map reduces the dimensions of the matrices
问题描述
说我有三个列表:
l_zero
[[1]]
[,1] [,2]
[1,] 0 0
[2,] 0 0
[[2]]
[,1] [,2]
[1,] 0 0
[2,] 0 0
l_ind <- list(matrix(c(1,1), ncol = 2), matrix(c(1,1,1,2), ncol = 2))
l_ind
[[1]]
[,1] [,2]
[1,] 1 1
[[2]]
[,1] [,2]
[1,] 1 1
[2,] 1 2
l_val <- list(5, c(4, 7))
l_val
[[1]]
[1] 5
[[2]]
[1] 4 7
我想在三个列表上运行Map
,目标是在l_zero
中用l_val
中的值替换l_ind
中的坐标零.
我的尝试给了我以下信息:
I would like to run Map
over the three lists with the goal of replacing in l_zero
the zeros with the coordinates in l_ind
with the values from l_val
.
My attempt gives me the following:
Map(function(l_zero, l_ind, l_val) l_zero[l_ind] <- l_val, l_zero = l_zero, l_ind = l_ind, l_val = l_val)
[[1]]
[1] 5
[[2]]
[1] 4 7
如您所见,矩阵的原始尺寸减小了,但是我想保留矩阵的尺寸,只是将值替换为l_ind
中的坐标.我尝试了l_zero[l_ind, drop = FALSE]
,但这也无济于事.
As you can see, the original dimensions of the matrices are reduced, but I would like to keep the dimensions of the matrices and just replace the values with the coordinates in l_ind
. I tried l_zero[l_ind, drop = FALSE]
, but that didn't help either.
有人可以帮我吗?
推荐答案
这里有一个更简单的方法,[<-
替换函数可以在Map()
的function参数中使用.按顺序需要三个参数.
Here's a bit simpler method, The [<-
replacement function can be used in Map()
's function argument. It takes three arguments, in order.
Map("[<-", l_zero, l_ind, l_val)
# [[1]]
# [,1] [,2]
# [1,] 5 0
# [2,] 0 0
#
# [[2]]
# [,1] [,2]
# [1,] 4 7
# [2,] 0 0
这篇关于运行图减小了矩阵的维数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!