Lukasz - 1 year ago 159

Python Question

I'm trying to construct a matrix of uniform distributions decaying to 0 at the same rate in each row. The distributions should be between -1 and 1. What I'm looking at is to construct something that resembles:

`[[0.454/exp(0) -0.032/exp(1) 0.641/exp(2)...]`

[-0.234/exp(0) 0.921/exp(1) 0.049/exp(2)...]

...

[0.910/exp(0) 0.003/exp(1) -0.908/exp(2)...]]

I can build a matrix of uniform distributions using:

`w = np.array([np.random.uniform(-1, 1, 10) for i in range(10)])`

and can achieve the desired result using a

`for`

`for k in range(len(w)):`

for l in range(len(w[0])):

w[k][l] = w[k][l]/np.exp(l)

but wanted to know if there was a better way of accomplishing this.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You can use numpy's broadcasting feature to do this:

```
w = np.random.uniform(-1, 1, size=(10, 10))
weights = np.exp(np.arange(10))
w /= weights
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**