-2

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?

  • 1
    For each column, start with a list containing an equal number of ones and zeroes then see this: https://stackoverflow.com/questions/42674509/how-to-shuffle-the-order-in-a-list – C. Pappy Mar 24 '22 at 21:31

1 Answers1

0

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
Ziur Olpa
  • 1,839
  • 1
  • 12
  • 27