How to set lower triangular matrix of 0-1?

arrays, matrix, numpy, python

Solution

You essentially want to increment the number of `1`s (starting from 0) in each row, while padding the rest of the row with `0`s, thereby keeping a constant length. Try something like this:

>>> n = 4
>>> [[1]*i + [0]*(n - i) for i in xrange(n)]
[[0, 0, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]

If you're using NumPy:

>>> import numpy as np
>>> np.tril(np.ones((n, n), dtype=int), -1)
array([[0, 0, 0, 0],
       [1, 0, 0, 0],
       [1, 1, 0, 0],
       [1, 1, 1, 0]])

Problem

I need to make a matrix, for n dimensions, to look like this for n=4: ``` [0,0,0,0] [1,0,0,0] [1,1,0,0] [1,1,1,0] ``` because I need the positions of the 1s, ie ``` 0, 1 0, 2 0, 3 1, 2 1, 3 2, 3 ``` This is because I want to work out the distances between x points, without wasting time repeating a distance. These coordinates will let me do it only once.

Original source