'Random binary matrix with specific number of ones
I want to create a random binary matrix with equal number of ones in each column
Any ideas, how to do in python with numpy for example?
Solution 1:[1]
Your imposition makes only possible to make matrix with an even number of rows, this may work:
half_n = 10
m = 5
binary_array = np.append(np.ones(half_n, dtype=int), np.zeros(half_n, dtype=int))
list_random = []
for _ in range(m):
binary_random = np.random.choice(binary_array, 2*half_n, replace=False)
list_random.append(binary_random)
matrix = np.matrix(list_random).T
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ziur Olpa |
