1

Suppose I have a code like this

probv=c(0.5,0.1,0.2,0.3)
N=c(1,2,3,4)

g1=matrix(rbinom(n = 10, size = N[1], prob = probv[1]), nrow=5)
g2=matrix(rbinom(n = 10, size = N[2], prob = probv[2]), nrow=5)
g3=matrix(rbinom(n = 10, size = N[3], prob = probv[3]), nrow=5)
g4=matrix(rbinom(n = 10, size = N[4], prob = probv[4]), nrow=5)

I want to use a for loop for i in (1:J) {......} J=4 in this case use one line function to return the same output like this, I want to know how I create a matrix g_ in the loop which is also benefit for me when I increase the length of my vector into 5,6,7...... for example N=c(1,2,3,4,5) probv=c(0.5,0.1,0.2,0.3,0.5) I do not change my code to create another matrix called g5.The code can create it and I just need to change my input to achieve my goal

Thanks Akrun

what is my N is a three dimensional array, I want to map the last dimension of it? How to change in the map method?

probv=c(0.5,0.1,0.2,0.3)
N=array(1:24,c(3,2,4))

g1=matrix(rbinom(n = 10, size = N[,,1], prob = probv[1]), nrow=5)
g2=matrix(rbinom(n = 10, size = N[,,2], prob = probv[2]), nrow=5)
g3=matrix(rbinom(n = 10, size = N[,,3], prob = probv[3]), nrow=5)
g4=matrix(rbinom(n = 10, size = N[,,4], prob = probv[4]), nrow=5)
5
  • Here, the lengths of 'probv' is 5 and last dimension of N is 4 Commented Jul 2, 2020 at 20:47
  • thanks, the first method is cool, and the second method looks like just produce 1 2by20 matrix Commented Jul 2, 2020 at 20:54
  • lst2 is a list of matrices (5 x 2) and the length of the list is 4. When we rbind, we get the 20 x2 because we are rbinding the list elements. I was not sure whether you need a single matrix of a list of matrices Commented Jul 2, 2020 at 20:56
  • sorry I need a list of matrices with g1,g2,g3,g4 Commented Jul 2, 2020 at 20:57
  • updated by assigning names to the list elements so that you can get the list elements by name. Commented Jul 2, 2020 at 20:59

1 Answer 1

1

We can use Map to loop over the 'N' and 'probv' vector, get the corresponding values into rbinom and create a matrix. It returns a list of matrices

lst1 <- Map(function(x, y) matrix(rbinom(n = 10,
        size = x, prob = y), nrow = 5), N, probv)

Or using for loop

lst2 <- vector('list', length(N))
for(i in seq_along(N)) {
     lst2[[i]] <- matrix(rbinom(n = 10, size = N[i], prob = probv[i]), nrow = 5)
   }
 names(lst2) <- paste0("g", seq_along(lst2))

For the updated question to extract from an array

mnLength <- min(length(probv), dim(N)[3])
lst2 <- vector('list', mnLength)
for(i in seq_len(mnLength)) {
     lst2[[i]] <- matrix(rbinom(n = 10, size = N[,,i], prob = probv[i]), nrow = 5)
   }

names(lst2) <- paste0("g", seq_along(lst2))
lst2$g1
lst2$g2
Sign up to request clarification or add additional context in comments.

4 Comments

thanks what if my N is an array and I want to map the last dimension of it.
```probv=c(0.5,0.1,0.2,0.3,0.5) N=array(1:24,c(3,2,4)) g1=matrix(rbinom(n = 10, size = N[,,1], prob = probv[1]), nrow=5) g2=matrix(rbinom(n = 10, size = N[,,2], prob = probv[2]), nrow=5) g3=matrix(rbinom(n = 10, size = N[,,3], prob = probv[3]), nrow=5) g4=matrix(rbinom(n = 10, size = N[,,4], prob = probv[4]), nrow=5)
For the second method, could you change it to produce 4 matrices ,instead of 1 matrix?thanks
@hardworker For the second, just use lst2. It is a list of 4 matrices. you don't need to rbind it to create mat1. Or convert to an array if you want an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.