jeudi 28 septembre 2023

randomly generate a matrix with fixed number of non zero values using python

I want to generate a random matrix (min value=0, max value=25, steps=5) with N rows and 6 columns, where the number of non-zero values (k) given as input.

Example:

Input: N=2 K=8

Output (result): [[ 5, 0, 20, 0, 15, 25], [ 0, 15, 15, 0, 25, 10]]

I tried to generate the matrix with the code: np.random.choice(np.arange(0,20, 5), size=(2, 6)) but I can't controll the number of non-zero value.




Aucun commentaire:

Enregistrer un commentaire