I need to fill up the matrix in the figure. My way is to use the loop which is not east to read. When the dimension is large, it not efficient too. Is there any way to make it easier to read and probably quicker (say using vectorization or some function I don't know?)
# The code is in python, but I am open to R as well.
S=6
p=[0.1,0.3,0.6] # the pi in the figure
# The reason I use loop for p is that it handles a flexible dimension of p
mat = np.zeros((S, S))
p = np.array(p)
for i in range(S):
for j, x in enumerate(p):
if i + j < S-1:
mat[i+j][i] = x
elif i + j == S-1:
mat[S-1][i] = p[j:].sum()
else:
pass
mat.T