Array indices must be positive integers or logical values.
1 次查看(过去 30 天)
显示 更早的评论
I tried to declare an if so that it would calculate a sign change in a vector and show the results but I get this error. If someone helps me I would be grateful
this is my code, variables min and max are introduced by user, i suposed is an error in the declaration of array but i don't know how fix it
tam=(min*-1)+xmax+1;
xi=(tam);
fxi=(tam);
for i=min:1:xmax
xi=min;
fev=inline(fx,'x');
fxi=fev(xi);
format long
r=sprintf('f( %d ) = %d', xi, fxi);
disp (r);
min=min+1;
end
disp(tam)
x1=0;
x2=0;
y1=0;
y2=0;
n=0;
for i=n:1:tam-1
if xi(n)*xi(n+1)<0
pos=i;
pos2=i+1;
y1=xi(pos);
y2=xi(pos2);
x1=fxi(pos);
x2=fxi(pos2);
disp(pos+1);
disp(pos2+1);
disp(x1);
disp(x2);
end
end
0 个评论
回答(2 个)
James Tursa
2020-6-12
编辑:James Tursa
2020-6-12
n = 0 and then you use it as an index:
n=0;
for i=n:1:tam-1
if xi(n)*xi(n+1)<0
You can't have a 0 index.
Also, min is the name of a MATLAB function ... it would be best if you picked a different name for your variable, such as xmin.
2 个评论
James Tursa
2020-6-12
java is 0-based indexing but MATLAB is 1-based indexing. You will need to add 1 to all of your indexing in your MATLAB code.
Image Analyst
2020-6-12
Finally. I was wondering when this question would come in today. So here is the link to this very faqqy of faqs:
Also wondering if you tried n = 1 or got rid of it entirely
for k = 1 : length(xi) - 1
if xi(k)*xi(k+1) < 0
pos = k;
pos2 = k + 1;
Of course it could be even better.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!