# [Numpy-discussion] Numpy newbie question: matrix creation

Robert Kern robert.kern at gmail.com
Wed Sep 25 08:10:16 EDT 2013

```On Wed, Sep 25, 2013 at 8:06 AM, Edmondo Porcu <edmondo.porcu at gmail.com>
wrote:
>
> Dear all,
>
> I am a Newbie with Numpy and I would need some advice on how to create a
matrix with certain characteristics :
>
>  - Every entry should be minimum 0 maximum 1 with a step of 0.1 (legal
values are 0,0.1,0.2,0.3 etc)
>
> - The number of columns of the matrix is a parameter of this matrix
creation algorithm
>
> - Only the rows where the sum is 1 must be kept
>

Use a multinomial distribution with n=10, then divide by 10.0.

For example, with 5 columns and 20 samples:

[~]
|4> np.random.multinomial(10, np.ones(5)/5.0, size=20) / 10.0
array([[ 0.1,  0.1,  0.4,  0.1,  0.3],
[ 0.2,  0.4,  0.1,  0.3,  0. ],
[ 0.3,  0.2,  0. ,  0.2,  0.3],
[ 0. ,  0.4,  0.4,  0. ,  0.2],
[ 0.2,  0.3,  0.1,  0.2,  0.2],
[ 0.1,  0.5,  0.1,  0.1,  0.2],
[ 0.4,  0. ,  0.2,  0.3,  0.1],
[ 0.3,  0.3,  0.1,  0. ,  0.3],
[ 0. ,  0.2,  0.5,  0.2,  0.1],
[ 0.5,  0.1,  0. ,  0.2,  0.2],
[ 0.1,  0.3,  0.2,  0.2,  0.2],
[ 0.3,  0.1,  0.2,  0.2,  0.2],
[ 0.2,  0.3,  0. ,  0.3,  0.2],
[ 0.2,  0.3,  0.2,  0.3,  0. ],
[ 0.1,  0.3,  0.2,  0. ,  0.4],
[ 0.1,  0.1,  0.2,  0.5,  0.1],
[ 0.1,  0.6,  0. ,  0. ,  0.3],
[ 0.1,  0.2,  0.4,  0.1,  0.2],
[ 0.3,  0.1,  0.2,  0.2,  0.2],
[ 0.2,  0.1,  0.4,  0.2,  0.1]])

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130925/0d99d1ed/attachment.html>
```