Index exceeds array bounds

2 次查看(过去 30 天)
Hesam Jokar
Hesam Jokar 2021-6-27
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 个评论
Hesam Jokar
Hesam Jokar 2021-6-28
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 2021-6-30
It is difficult to say anythig without seing the whole code.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2021-6-27
  2 个评论
Image Analyst
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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by