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

2 views (last 30 days)
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);

Answers (1)

Richard Brown
Richard Brown on 9 Jul 2012
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

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!