我需要填充图中的矩阵。我的方法是使用非东方的循环读取。当尺寸较大时,效率也不高。 有什么办法,使其更容易阅读和可能会比较快(?说使用矢量或者一些功能我不知道)更直观的方法来填充对角线向量右边的三个元素
# 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
[这](http://stackoverflow.com/questions/5852495/how-do-i-find-the -n-numpy-matrix -scalar-product-of-numpy-matrix)将会很有帮助。另外,如果使用'for',则在'for'之后更改黄色部分的值。不需要在内部使用'if'作为',这对于只有两个改变是很麻烦的。 –