0
我最近一直在試圖將一段Matlab代碼轉換爲Python代碼。Matlab to Python轉換 - 無法分配給函數調用
我已經做大多數的,我需要但是,我遇到的問題是,它說行了變化:
y(index(m)) = 1-x(index(m));
我得到的錯誤:
「不能分配以函數調用「
但我不知道如何重組它以便消除此錯誤。
我看了一下,人們提到「get item」和「set item」,但是我嘗試過使用它們,但我無法讓它們工作(可能是因爲我無法弄清楚結構)
下面是完整的代碼:
import numpy
N = 100;
B = N+1;
M = 5e4;
burnin = M;
Niter = 20;
p = ones(B,Niter+1)/B;
hit = zeros(B,1);
for j in range(1,Niter):
x = double(rand(1,N)>0.5);
bin_x = 1+sum(x);
index = ceil(N*rand(1,M+burnin));
acceptval = rand(1,M+burnin);
for m in range(1,M+burnin):
y = x;
y(index(m)) = 1-x(index(m));
bin_y = 1+sum(y);
alpha = min(1, p(bin_x,j)/p(bin_y,j));
if acceptval(m)<alpha:
x = y; bin_x = bin_y;
end
if m > burnin: hit(bin_x) = hit(bin_x)+1; end
end
pnew = p[:,j];
for b in range(1,B-1):
if (hit(b+1)*hit(b) == 0):
pnew(b+1) = pnew(b)*(p(b+1,j)/p(b,j));
else:
g(b,j) = hit(b+1)*hit(b)/(hit(b+1)+hit(b));
g_hat(b) = g(b,j)/sum(g(b,arange(1,j)));
pnew(b+1) = pnew(b)*(p(b+1,j)/p(b,j))+((hit(b+1)/hit(b))^g_hat(b));
end
end
p[:,j+1] = pnew/sum(pnew);
hit[:] = 0;
end
在此先感謝
非常感謝你,你一直在給我很大的幫助,我現在就通過它來試着理解你所做的一切。再次感謝 – 2013-03-13 16:22:48