Index exceeds array bounds

4 views (last 30 days)
Hesam Jokar
Hesam Jokar on 27 Jun 2021
Commented: Image Analyst on 30 Jun 2021
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
Hesam Jokar
Hesam Jokar on 28 Jun 2021
thanks. I have defined the Tfodeg(t,i) before the for loops. You think i have to define it inside the loop?
G A
G A on 30 Jun 2021
It is difficult to say anythig without seing the whole code.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 27 Jun 2021
  2 Comments
Image Analyst
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));

Sign in to comment.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!