Attempted to access Pe(7); index must be a positive integer or logical.

조회 수: 1 (최근 30일)
Can
Can 2012년 7월 8일
I wrote this function but I get an error:
??? Attempted to access Pe(7); index must be a positive integer or logical.
Error in ==> prob3 at 56
Pe(m)=(Pe(m)+error(j))/10000;
What should I do for it?
Pe=zeros(1,11);
x=zeros(1,10000);
y=zeros(1,10000);
z=zeros(1,10000);
error=zeros(1,10000);
for q=0:0.1:1
for k=1:10000
error(k)=0;
end
for i=1:10000
if rand(1)<1/2
x(i)=0;
if rand(1)<q
y(i)=1;
if q<1/2
z(i)=1;
else
z(i)=0;
end
else
y(i)=0;
if q>1/2
z(i)=1;
else
z(i)=0;
end
end
else
x(i)=1;
if rand(1)<q
y(i)=1;
if q<1/2
z(i)=1;
else
z(i)=0;
end
else
y(i)=0;
if q>1/2
z(i)=1;
else
z(i)=0;
end
end
end
if x(i)==z(i)
error(i)=error(i)+0;
else
error(i)=error(i)+1;
end
end
m=(q./0.1)+1;
for j=1:10000
Pe(m)=(Pe(m)+error(j))/10000;
end
end
plot(Pe,q);

답변 (1개)

Richard Brown
Richard Brown 2012년 7월 9일
Your problem is that q and hence m are floating point numbers, not integers. in this case the problem is that
m = 0.6 / 0.1 + 1
differs from 7 in the sixteenth decimal place, and hence can't be used to index. Either use
m = round(q / 0.1 + 1)
or, better, use integer indices in your loop, and compute q from them

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by