samedi 3 décembre 2022

Generate random binary matrix constrained to no null row

I want to generate a random binary matrix, so I'm using W=np.random.binomial(1, p, (n,n)). It works fine, but I want a constraint that no row is just of 0s.

I create the following function:

def random_matrix(p,n):
m=0
while m==0:
    W = np.random.binomial(1, p, (n,n))
    m=min(W.sum(axis=1))
return W

It also works fine, but it seems to me too inefficient. Is there a faster way to create this constraint?




Aucun commentaire:

Enregistrer un commentaire