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!