我是MatLab的超级初学者,我正在尝试调试我正在编写的一个简单脚本。在尝试调试我的代码时出现奇怪的错误。这里的脚本:为什么0 + 1 == 49?
function [prob] = QuantumHW1(j,k,m)
X = [0 1; 1 0];
Y = [0 -sqrt(-1); sqrt(-1) 0];
Z = [1 0; 0 -1];
H = 1/sqrt(2) * [1 1; 1 -1];
S = [1 0; 0 i];
T = [1 0; 0 exp(sqrt(-1)*pi/4)];
mats = {X,Y,Z,H,S,T};
binJ = dec2bin(j,k);
binM = dec2bin(m,k);
totOps = {};
%Set up all the operators to be used
for p = 1:k
totOps(p) = mats(mod(p,6));
if p == 0
totOps(p) = X;
end
end
withM = {};
%Dot product with M
for p = 1:k
p
binM(p)+1
totOps(:,1)
withM(p) = totOps(:,binM(p)+1);
end
rTotal = 0;
%Now take components with respect to J
for p = 1:k
rTotal = rTotal + [not(binJ(p)),binJ(p)] * withM(p);
end
prob = norm(runningTotal)^2;
disp('The probability to measure j = %d in a k = %d system on input m = %d is %d',j,k,m,prob);
end
当我运行程序时,我得到一个数组界限错误的指标就行了withM(p) = totOps(:,binM(p)+1);
。我试图确保p的值是正确的。在for循环的第一次迭代中,binM(p)= 0。但是当我尝试获取binM(p)+1时,我得到了49.这太奇怪了。
任何帮助,非常感谢。我正在试图弄清楚为什么会出现这种情况,我正在靠墙敲打我的头。
+1有趣的问题。 :) – yuk 2012-02-14 16:22:31
标题是有点误导。 – 2012-06-10 18:43:47