0

How can I create a binary matrix where each column sums to 1 and in the columns is >= 1? I need to build n*m matrices to perform assignments in an algorithm.

user21559
  • 7
  • 2

1 Answers1

0

I don't know if this is what you are looking for, but an identity matrix solve your request if the number of rows is equal to the number of columns.

With numpy, you can achieve that

import numpy as np

rows = 3
matrix = np.eye(rows)

The result is then :
[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]

vighub
  • 173
  • 1
  • 6
  • And in case the matrix is not 2^n, how could you do it? – user21559 Mar 21 '23 at 13:02
  • If the number of columns is different from the number of rows, it is not possible to have at least one element in every column equal to 1, and at maximum one element for every row equal to 1. You can verify this starting from a square identity matrix, that fulfill you request. Say for example you have a identity matrix of 3 rows and cols. Now you add a column, making it a 3x4 matrix. There is no way you can set an element of the last column to 1 without having two elements set to 1 in a row. – vighub Mar 21 '23 at 13:07
  • Yes, what I am looking for is that for each row there is only one 1 and for columns that there is at least one 1 – user21559 Mar 21 '23 at 14:01