これはMATLABで機能します。
>> p = [1, 0, 2, 4, 3, 6, 5];
>> p(p+1)
ans =
0 1 2 3 4 5 6
NumPyで同じことをする方法はありますか?方法がわかりません:
>>> p = mat([1, 0, 2, 4, 3, 6, 5])
>>> p[p]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 305, in __getitem__
out = N.ndarray.__getitem__(self, index)
IndexError: index (1) out of range (0<=index<0) in dimension 0
>>> p[:,p]
この時点で、インタプリタは無限ループに入っているようです。これにより、無限ループも発生します。
>>> [p[:,i] for i in p]
しかし、これは機能します:
>>> [p[:,i] for in range(0,6)]
したがって、問題の原因となるのは、行列メンバーを独自のインデックスとして使用することです。これはPythonのバグですか?それとも私は何か間違ったことをしていますか?