Index exceeds array bounds
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Image Analyst
on 27 Jun 2021
See the FAQ for a thorough discussion of the error:
2 Comments
Image Analyst
on 30 Jun 2021
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));
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!