I'm not quite sure I understand the question. Are you asking for a binary representation with the correct number of leading zeroes? So the three column matrix would get 000, 001, 010, ... 111. If that's the case, use this to get rid of the extra zeroes instead of the s///: