修复R中的foreach和dopar循环。

huangapple go评论97阅读模式
英文:

fixing foreach and dopar loop in R

问题

以下是您要翻译的内容:

"Following the previous posthere, I wrote a code in R, but it does not work.

What I want to do is to have multiple output of arrays with foreach.

In Matlab, what I want to do is as follows. I want to have arrays X and Y by parellel computing.

X=zeros(2,5,10); Y=zeros(10,2); parfor i=1:10; X(:,:,i) = randn(2,5); Y(i,:) = randn(1,2); end

Based on the comments for the previous post, I wrote an R code as follows. But, this does not work. The problem seems to be around the definition of Z.

  1. X <- array(0, dim = c(2,5,10)) Y <- array(0, dim = c(10,2)) Z <- foreach(i=1:10, .combine = 'c') %dopar% { Y_i <- i*c(1,2) X_i <- matrix(rnorm(10),2,5) Y[ i , ] <- Y_i X[ , , i] <- X_i Z <- list(Y, X) } ```
  2. Any help would be much appreciated."
  3. <details>
  4. <summary>英文:</summary>
  5. Following the previous post[here](https://stackoverflow.com/q/75479731/21230830), I wrote a code in R, but it does not work.
  6. What I want to do is to have multiple output of arrays with `foreach`.
  7. In Matlab, what I want to do is as follows. I want to have arrays X and Y by parellel computing.

X=zeros(2,5,10);
Y=zeros(10,2);
parfor i=1:10;
X(:,:,i) = randn(2,5);
Y(i,:) = randn(1,2);
end

  1. Based on the comments for the previous post, I wrote an R code as follows. But, this does not work. The problem seems to be around the definition of Z.

rm(list=ls()) # clear all variables
library(foreach)
library(doParallel)

X <- array(0, dim = c(2,5,10))
Y <- array(0, dim = c(10,2))
Z <- foreach(i=1:10, .combine = 'c') %dopar% {
Y_i <- i*c(1,2)
X_i <- matrix(rnorm(10),2,5)
Y[ i , ] <- Y_i
X[ , , i] <- X_i
Z <- list(Y, X)
}

  1. Any help would be much appreciated.
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 给定的代码几乎完成了。在循环内部,它涉及环境(参见优秀的解释:http://adv-r.had.co.nz/Environments.html)。因此,就像一个函数一样,它需要将 Z(局部环境)返回给 Z(全局环境)。
  6. ```R
  7. rm(list=ls()) # 清除所有变量
  8. library(foreach)
  9. library(doParallel)
  10. X <- array(0, dim = c(2,5,10))
  11. Y <- array(0, dim = c(10,2))
  12. Z <- foreach(i=1:10, .combine = 'c') %dopar% {
  13. Y_i <- i*c(1,2)
  14. X_i <- matrix(rnorm(10),2,5)
  15. Y[ i , ] <- Y_i
  16. X[ , , i] <- X_i
  17. Z <- list(Y, X)
  18. Z
  19. }
英文:

The given code is almost done. Inside the loop, it is about environment (see the excellent explanation: http://adv-r.had.co.nz/Environments.html). Thus, like a function, it needs to return Z (local env) to Z (global env).

  1. rm(list=ls()) # clear all variables
  2. library(foreach)
  3. library(doParallel)
  4. X &lt;- array(0, dim = c(2,5,10))
  5. Y &lt;- array(0, dim = c(10,2))
  6. Z &lt;- foreach(i=1:10, .combine = &#39;c&#39;) %dopar% {
  7. Y_i &lt;- i*c(1,2)
  8. X_i &lt;- matrix(rnorm(10),2,5)
  9. Y[ i , ] &lt;- Y_i
  10. X[ , , i] &lt;- X_i
  11. Z &lt;- list(Y, X)
  12. Z
  13. }

huangapple
  • 本文由 发表于 2023年2月18日 07:52:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定