0

For a matrix of M by N containing just 0 and 1, how to make no identical rows exists. For example, if N = 3 and M = 3, one solution is:

0 0 0
0 1 0
1 0 0

I know using some exhausive permutation algorithm works, but I am thinking is there any better solution since M < 2^N is the common case.

Bs He
  • 717
  • 1
  • 10
  • 22
  • 1
    View each row as a number in binary. As long as each row is a unique binary number, it will be unique. So get numbers, `sample(0:(2^N - 1), size = M, replace = FALSE)`, convert them to binary strings (with leading zeros) and split the strings into the columns of your matrix. – Gregor Thomas Aug 17 '17 at 16:58
  • @Gregor Looks pretty neat. But why I need leading zeros? – Bs He Aug 17 '17 at 17:01
  • got it, to make each row have same number of elements. – Bs He Aug 17 '17 at 17:05

0 Answers0