"Subscript indices must either be real positive integers or logicals"

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!

 采纳的回答

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

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!
My pleasure!
The sincerest expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.

请先登录,再进行评论。

更多回答(1 个)

Haha, that is indeed stupid and quite embarrassing. I totally forgot about the two n's. Thank you, that did solve my problem!

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by