Re: [R] creating lists of random matrices

2016-11-09 Thread Peter Langfelder
Add a simplify = FALSE to the call to replicate, and you'll get a list. replicate(5, matrix(rnorm(4), 2, 2), simplify = FALSE) Peter On Wed, Nov 9, 2016 at 10:45 AM, Rui Barradas wrote: > Hello, > > I also thought of replicate() but it creates an 2x2x5 array, not a list. > Maybe it's all the

Re: [R] creating lists of random matrices

2016-11-09 Thread Rui Barradas
Hello, I also thought of replicate() but it creates an 2x2x5 array, not a list. Maybe it's all the same for the OP. Rui Barradas Em 09-11-2016 18:41, Marc Schwartz escreveu: On Nov 9, 2016, at 12:32 PM, Evan Cooch wrote: So, its easy enough to create a random matrix, using something like (

Re: [R] creating lists of random matrices

2016-11-09 Thread Uwe Ligges
Not answering the question, but if you have the same dimensions of the matrices everywhere, it is much more efficient to sample all numbers in a row and put stuff into an array: array(rnorm(5*4), dim=c(2,2,5))) Best, Uwe Ligges On 09.11.2016 19:51, Marc Schwartz wrote: On Nov 9, 2016, a

Re: [R] creating lists of random matrices

2016-11-09 Thread Evan Cooch
>> >> Hi, >> >> See ?replicate >> >> Example: >> >> ## Create a list of 5 2x2 matrices > > > Sorry, correction on my reply. > > I copied the wrong output, It should be: > > > replicate(5, matrix(rnorm(4), 2, 2), simplify = FALSE) > Perfect does the trick. Thanks also for the lapply solutions fro

Re: [R] creating lists of random matrices

2016-11-09 Thread Marc Schwartz
> On Nov 9, 2016, at 12:41 PM, Marc Schwartz wrote: > > >> On Nov 9, 2016, at 12:32 PM, Evan Cooch wrote: >> >> So, its easy enough to create a random matrix, using something like (say) >> >> matrix(rnorm(4),2,2) >> >> which generates a (2x2) matrix with random N(0,1) in each cell. >> >> B

Re: [R] creating lists of random matrices

2016-11-09 Thread Rui Barradas
Hello, Try using ?lapply, its return value is a list. Note however that the *apply functions are loops in disguise. There's nothing wrong with your solution to the problem, but it's true that most R users find lapply more elegant, like you say. hold <- lapply(1:5, function(x) matrix(rnorm(4)

Re: [R] creating lists of random matrices

2016-11-09 Thread Marc Schwartz
> On Nov 9, 2016, at 12:32 PM, Evan Cooch wrote: > > So, its easy enough to create a random matrix, using something like (say) > > matrix(rnorm(4),2,2) > > which generates a (2x2) matrix with random N(0,1) in each cell. > > But, what I need to be able to do is create a 'list' of such random m

[R] creating lists of random matrices

2016-11-09 Thread Evan Cooch
So, its easy enough to create a random matrix, using something like (say) matrix(rnorm(4),2,2) which generates a (2x2) matrix with random N(0,1) in each cell. But, what I need to be able to do is create a 'list' of such random matrices, where the length of the list (i.e., the number of said ra