Index exceeds array bounds

5 ビュー (過去 30 日間)
Hesam Jokar
Hesam Jokar 2021 年 6 月 27 日
コメント済み: Image Analyst 2021 年 6 月 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 件のコメント
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 件のコメント
Hesam Jokar
Hesam Jokar 2021 年 6 月 28 日
Thanks
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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by