Sample random matrix

Hi all,

I’m trying to sample a random matrix, where each element is independent. For example, if I have a matrix of means

M = [1. 10.; 100. 1000.]

and I want to generate data, where each element is sampled as an exponential with mean corresponding to the element of M. The following roughly works

D = map(x -> rand(Exponential(x), 1), M)

This generates an array of arrays though, instead of a 2x2 array (matrix). Any cleaner way to generate a matrix, instead of having to convert the array of arrays to the appropriate shape?

Note that your original code almost does what you want, just call rand like this:

using Distributions
M = [1. 10.; 100. 1000.]
map(x -> rand(Exponential(x)), M)

That said, broadcasting should lead to more compact syntax (but you may need v0.6 for this, did not check in v0.5.2):

rand.(Exponential.(M))
2 Likes

Thanks so much @Tamas_Papp - that works perfectly!

You may also be interested in RandomMatrices

1 Like