"Subscript indices must either be real positive integers or logicals" error. unable to understand why?
I have these 3 functions
function r = RingRead(n,p)
r=( p * RingReadggg(n-1,p))+( (1-p)* RingReadggb(n-1,p));
end
function r = RingReadggb(n , p)
if n <= 1
r = 0;
else
r = ((1-p)* RingReadggb(n-1,p) )+ p^2 +( p(1-p)* RingReadggb(n-2,p));
end
end
function r = RingReadggg(n , p)
if n == 1
r = p;
else
r = (p+p(1-p)开发者_StackOverflow社区+( (1-p)^2 * RingReadggb(n-2,p)));
end
end
Below given program uses above given functions.
for p = 0.50:0.05:1
r = RingRead(4,p);
plot(p,r)
hold on
end
When i run this it gives error
??? Subscript indices must either be real positive integers or logicals.
Error in ==> RingRead>RingReadggg at 18 r = (p+p(1-p)+( (1-p)^2 * RingReadggb(n-2,p)));
Error in ==> RingRead at 3 r=( p * RingReadggg(n-1,p))+( (1-p)* RingReadggb(n-1,p));
Error in ==> RingAvailability at 2 r = RingRead(4,p);
Your code crashes right here:
r = (p+p(1-p)+( (1-p)^2 * RingReadggb(n-2,p)))
n is 3 and p is 0.5
but what is
p(1-p)
supposed to be? do you mean p * (1-p)
?
As the message says, the error occurs on this line:
r = (p+p(1-p)+( (1-p)^2 * RingReadggb(n-2,p)));
By p(1-p)
, do you really mean p * (1-p)
? Entered as p(1-p)
is interpreted as indexing into p
at the index 1-p
. Try changing the line to:
r = (p+p*(1-p)+( (1-p)^2 * RingReadggb(n-2,p)));
It looks like you have the same issue in RingReadggb as well.
精彩评论