Index exceeds array bounds
2 次查看(过去 30 天)
显示 更早的评论
i have written a code which gets data at time t and location i in two for loops, and calculates parameters. i haven't assigned a size to the arrays.
for t=1:24
Tfodeg(1,1)=20;Tfo(1,1)=293.15;Tfodeg(2,1)=20;Tfo(2,1)=293.15;Tpmh(1,1)=293;
for i=1:16
rho_i(t,i)=999.9-((4.48e-3).*(Tfodeg(t,i-1).^2));
mu_i(t,i)=0.001.*exp(-1.6-(1150./Tfo(t,i-1))+((690/Tfo(t,i-1))^2));
first I have assigned the preliminiary conditions. when i run this, i get the error:
Index in position 2 is invalid. Array indices must be positive integers or logical values.
which was rational. so, i changed the code
for t=2:25
Tfodeg(1,1)=20;Tfo(1,1)=293.15;Tfodeg(2,1)=20;Tfo(2,1)=293.15;Tpmh(1,1)=293;
for i=2:17
rho_i(t,i)=999.9-((4.48e-3).*(Tfodeg(t,i).^2));
mu_i(t,i)=0.001.*exp(-1.6-(1150./Tfo(t,i))+((690/Tfo(t,i))^2));
and i get this one:
Index in position 2 exceeds array bounds (must not exceed 1).
I think my code is mistake-free. anyone can help?
3 个评论
回答(1 个)
Image Analyst
2021-6-27
See the FAQ for a thorough discussion of the error:
2 个评论
Image Analyst
2021-6-30
You say "I have defined the Tfodeg(t,i) before the for loops." but what are its dimensions. Before the for loop, do this
sizeTfo = size(Tfodeg) % No semicolon
for t=2:25
Tfodeg(1,1)=20;Tfo(1,1)=293.15;Tfodeg(2,1)=20;Tfo(2,1)=293.15;Tpmh(1,1)=293;
for i=2:17
rho_i(t,i)=999.9-((4.48e-3).*(Tfodeg(t,i).^2));
mu_i(t,i)=0.001.*exp(-1.6-(1150./Tfo(t,i))+((690/Tfo(t,i))^2));
What does it say? My guess is that the second dimension is only 1m,, not 15. So to fix, either make it 15 or 16 or limit your inner for loop to the number of columns it has:
sizeTfo = size(Tfo) % No semicolon
sizeTfodeg = size(Tfodeg) % No semicolon
for t=2:25
Tfodeg(1,1)=20;
Tfo(1,1)=293.15;
Tfodeg(2,1)=20;
Tfo(2,1)=293.15;
Tpmh(1,1)=293;
lastIndex = min([sizeTfo(2), sizeTfodeg(2)])
for i=2 : lastIndex
rho_i(t,i)=999.9-((4.48e-3).*(Tfodeg(t,i).^2));
mu_i(t,i)=0.001.*exp(-1.6-(1150./Tfo(t,i))+((690/Tfo(t,i))^2));
另请参阅
类别
在 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!