I need to generate an m*n matrix with 1 and 0 where the sum of each row is 1 and each column is greater than or equal to 1. I'm trying this code but I don't know how to limit my condition on the columns, since sometimes I get zero columns and I need them all to have at least one 1. How could I limit the sum in the columns to be at least 1?
pt = []
sumr = 0
sumc = []
for i in range(n):
quantity = random.choices([1])
row = np.zeros(p, dtype=int)
idx = np.random.choice(range(p), quantity, replace=False)
row[idx] = 1
pt.append(row)
for j in range(len(pt[0])):
sumc.append(sum([row[j] for row in pt]))
print(sumc)
I would just create a matrix with ones in random places, and then check if there are still zero columns once we near the end so we don't leave any empty:
However, this can be simplified if we make use of an identity matrix: