"Subscript indices must either be real positive integers or logicals"
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I'm not sure if this is the right title, but I ran into a problem with my current code.
speed=[1511 1505.7 1490.2 1484.1 1480.3 1481.8 1485.4 1491 1506.3 1524 1548.6];
depth=[0 85 165 295 665 1005 1505 2005 3005 4005 5000];
phi=20;
i=1;
r=0;
while r<4000
n=1;
xn=1;
for i=1:11
c1=speed(1,i);
z1=depth(1,i);
z2=depth(1,i+1);
c2=speed(1,i+1);
a=sind(phi)/c1;
for z=z1:z2
m=(c2-c1)/(z2-z1);
n=c1-m*z1;
cn=m*z+n;
theta=asind(a*cn);
dx=(z-(z+1))*tand(theta);
xn=xn+dx
x(1,n)=abs(xn);
y(1,n)=z;
n=n+1
r=abs(xn);
end
end
phi=phi+5;
end
However, if I run it, I get the error
Subscript indices must either be real positive integers or logicals.
Error in ATOC (line 83)
x(1,n)=abs(xn);
which I don't understand, because n should be a positive integer...
Any ideas? Thank you very much!
댓글 수: 0
채택된 답변
Star Strider
2015년 10월 31일
You have two definitions of ‘n’ in your loop, first:
n=c1-m*z1;
then later:
n=n+1;
The first one is creating fractional values for ‘n’, and that is what is throwing the error. It occurs when the lengths of ‘x’ and ‘y’ each have 1511 elements. Since I do not know what you are doing, I cannot offer any specific solution.
댓글 수: 2
Star Strider
2015년 11월 1일
Anke Kügler’s ‘Answer’ is copied here:
Haha, that is indeed stupid and quite embarrassing. I totally forgot about the two n's. Thank you, that did solve my problem!
Star Strider
2015년 11월 1일
My pleasure!
The sincerest expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.
추가 답변 (1개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!