I have an array A with shape (N,). I am taking N=5 for illustration:
A = np.array([0,1,1,0,1])
And I want to transform it to the following NxN matrix B. Solutions in both NumPy and Tensorflow are good but the latter is preferred.
B = np.array([[0,1,1,0,1],
[0,1,1,0,1],
[0,1,1,0,1],
[0,0,0,0,1],
[0,0,0,0,1]])
One solution can be comprised of following steps:
- Repeat array
AN times - Loop through each row
i. Look for the index of the last zero until thei-thelement of that row. - Replace all elements preceding that index with zeros.
Another illustration with N=10:
D = np.array([0,1,1,1,0,0,1,1,0,0])
E = np.array([[0,1,1,1,0,0,1,1,0,0],
[0,1,1,1,0,0,1,1,0,0],
[0,1,1,1,0,0,1,1,0,0],
[0,1,1,1,0,0,1,1,0,0],
[0,0,0,0,0,0,1,1,0,0],
[0,0,0,0,0,0,1,1,0,0],
[0,0,0,0,0,0,1,1,0,0],
[0,0,0,0,0,0,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]])