# Sample random matrix

**URL:** <https://discourse.julialang.org/t/sample-random-matrix/3789>\
**Category:** Statistics\
**Created:** [May 18, 2017, 2:50pm UTC](https://discourse.julialang.org/t/sample-random-matrix/3789 "2017-05-18T14:50:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [May 18, 2017, 2:50pm UTC](https://discourse.julialang.org/t/sample-random-matrix/3789/1 "2017-05-18T14:50:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 18, 2017, 2:56pm UTC](https://discourse.julialang.org/t/sample-random-matrix/3789/2 "2017-05-18T14:56:20Z")

</div>

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

```julia
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`):

```julia
rand.(Exponential.(M))

```

---

<div class="post-metadata">

**Author:** ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)\
**Post date:** [May 18, 2017, 3:00pm UTC](https://discourse.julialang.org/t/sample-random-matrix/3789/3 "2017-05-18T15:00:18Z")

</div>

Thanks so much @Tamas_Papp - that works perfectly!

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 18, 2017, 3:29pm UTC](https://discourse.julialang.org/t/sample-random-matrix/3789/4 "2017-05-18T15:29:18Z")

</div>

You may also be interested in [RandomMatrices](https://github.com/jiahao/RandomMatrices.jl)
