Sample random matrix


#1

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?


#2

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))

#3

Thanks so much @Tamas_Papp - that works perfectly!


#4

You may also be interested in RandomMatrices