D*********2 发帖数: 535 | 1 不好意思麻烦下各位R高手~
我现在有两个矩阵,A, B, 还有一个同样的概率矩阵,要从A,B中抽样建一个新的矩阵U
。U中的每一个element, 都是从A ,B中的对应位置依bernoulli(P)抽样,P也是同样的
对应位置。
按理说是很整齐的格式。但R里面这个sample好像只能处理按元素处理?
有没有什么省时的方法,谢谢谢谢。
> (A <- matrix(1:12, 4, 3))
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> (B <- matrix(rep(c(1:3),4), 4, 3))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 1
[3,] 3 1 2
[4,] 1 2 3
> (P <- matrix(runif(12), 4, 3))
[,1] [,2] [, | t**i 发帖数: 688 | 2 (1) Based on P matrix, generate a matrix of Bernouli sample, point-by-point
in correspondence with P matrix. This matrix, S, has either 0 or 1 at every
element.
(2) Obtain a SS matrix such that SS = matrix of 1's everywhere - S
(3) Obtain A*S + B*SS. | D*********2 发帖数: 535 | 3
point
every
Can (1) be done in matrix form? I mean if there is still a loop involved, it
is relatively slow.
【在 t**i 的大作中提到】 : (1) Based on P matrix, generate a matrix of Bernouli sample, point-by-point : in correspondence with P matrix. This matrix, S, has either 0 or 1 at every : element. : (2) Obtain a SS matrix such that SS = matrix of 1's everywhere - S : (3) Obtain A*S + B*SS.
| o****o 发帖数: 8077 | 4 for (1), you can do
(matrix(runif(12), 4, 3)>P)*1
it
【在 D*********2 的大作中提到】 : : point : every : Can (1) be done in matrix form? I mean if there is still a loop involved, it : is relatively slow.
| D*********2 发帖数: 535 | 5
Thank you!!!!! Exactly what I need.
【在 o****o 的大作中提到】 : for (1), you can do : (matrix(runif(12), 4, 3)>P)*1 : : it
|
|