Array indices must be positive integers or logical values.

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

回答(2 个)

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 个评论

yeah, i tried but i get the same error (
Array indices must be positive integers or logical values.
Error in title (line 53)
if (xi(n))*(xi(n+1))<=0 )
i'm principiant and i'm learning alone
I ask me if exist an easy form to do operations with the value into an especific position of vector. I try to convert code of java to an matlab script, this is the part that i try to implement
rfx is an array his lenght is declareted for a sum of two values writen by the user
for (int it=0; it<rr-1;it++){
if( rfx [ it ]*rfx [ it+1 ]<=0){
int pos=it;
int pos2=it+1;
y1=rfx[pos];
y2=rfx[pos2];
xx1=rfxi[pos];
xx2=rfxi[pos2];
System.out.println("Solucion");
System.out.println(pos+1);
System.out.println(pos2+1);
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.

请先登录,再进行评论。

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.

类别

帮助中心File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by