Attempted to access stepcount(0); index must be a positive integer or logical.

1 vue (au cours des 30 derniers jours)
Siyao Sui
Siyao Sui le 4 Mai 2016
Modifié(e) : Weird Rando le 5 Mai 2016
I get the error message:"Attempted to access stepcount(0); index must be a positive integer or logical." But I've already set z11=floor(real(z1)) to make sure it's a positive integer, can someone help?
Here is my code:
a=input('range from = ');
b=input('range to = ');
N=input('pairs of complex number = ');
gcdcount=zeros(1,1);
stepcount=zeros(1,1);
for j=1:M
z1=a + (b-a).*rand(N,1)+ 1 + a + (b-a).*rand(N,1)*1i;
z2=a + (b-a).*rand(N,1)+ 1 + a + (b-a).*rand(N,1)*1i;
z11=floor(real(z1));
end
if(abs(z1)<abs(z2)) % switch z1 and z2, if necessary, so that b<a
c=z1; % hang onto the value of a
z1=z2; % even while replacing a with b
z2=c; % now replace b with a
end
count=0; % initialize counter
while(abs(z2)>0)
u=z1;
v=z2;
z1=z2;
q=(u/v);
q1=real(q);
q2=imag(q);
if (q1-floor(q1)<=0.5)
q1=floor(q1);
else
q1=1+floor(q1);
end
if (q2-floor(q2)<=0.5)
q2=floor(q2);
else
q2=1+floor(q2);
end
if(length(gcdcount) >=z11)
gcdcount(z11)=gcdcount(z11)+1; %increment appropriate counter
else
gcdcount(z11)=1;
end
if(length(stepcount) >=count)
stepcount(count)=stepcount(count)+1; %increment appropriate counter
else
stepcount(count)=1;
end
if(length(gcdcount) >=z11)
gcdcount(z11)=gcdcount(z11)+1; %increment appropriate counter
else
gcdcount(z11)=1;
end
if(length(stepcount) >=count)
stepcount(count)=stepcount(count)+1; %increment appropriate counter
else
stepcount(count)=1;
end
end
subplot(2,1,1)
plot(gcdcount/M)
title('Distribution of gcds')
subplot(2,1,2)
plot(stepcount/M)
title('Distribution of algorithm steps')
subplot(111) % means the figure window returns to normal single-graph behavior
  2 commentaires
CS Researcher
CS Researcher le 4 Mai 2016
floor is not the best option. Use ceil instead.
Siyao Sui
Siyao Sui le 4 Mai 2016
I tried, it still gave me the same error message

Connectez-vous pour commenter.

Réponses (4)

Azzi Abdelmalek
Azzi Abdelmalek le 4 Mai 2016
stepcount(count) gives an error because count is initialized to 0
  4 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 4 Mai 2016
Modifié(e) : Azzi Abdelmalek le 4 Mai 2016
I can't tell you what to do, because I don't know the aim of your code. Also your counter count is not incremented in your code. What the following loop is doing?
for j=1:M
z1=a + (b-a).*rand(N,1)+ 1 + a + (b-a).*rand(N,1)*1i;
z2=a + (b-a).*rand(N,1)+ 1 + a + (b-a).*rand(N,1)*1i;
z11=floor(real(z1));
end
each loop, your variables are erased!
I think, you need to revise all your code.
Siyao Sui
Siyao Sui le 5 Mai 2016
I want to randomly generate N pairs of complex numbers, find their gcds and plot distribution of gcds and distribution of algorithm steps taken.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 4 Mai 2016
MATLAB uses 1-based indexing so the first element in a matrix is element 1. This is different from languages that use 0-based indexing, where the first element is element 0. You will need to adjust your code so you don't try to access or write to element 0 of a matrix. The easiest way to do so (if you wrote your code assuming 0-based indexing) is to add 1 to your indices.

Image Analyst
Image Analyst le 5 Mai 2016
Try replacing this
count=0; % initialize counter
with this
count = 1; % Initialize loop iteration counter
and see if the rest of the code works after that.

Weird Rando
Weird Rando le 5 Mai 2016
Modifié(e) : Weird Rando le 5 Mai 2016
for j=1:M
exactly what is M? Cause it was not previously defined in your code

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by