Index in position 1 exceeds array bounds. Index must not exceed 1.

I am getting Index exceeding array size in Matlab. Can someone see the code and help. Thanks
E = zeros(1,1000);
for j = 2:9
n = 1;
E(j,n) = ((x(j,n)-x((j-1),n))*P(n,(j-1))) + ((y(j,n)-y((j-1),n))*Q(n,(j-1)))
F(j,n) = ((x(j,n)-x((j-1),n))*(P(j,n)+R((j-1),n))) + ((y(j,n)-y((j-1),n))*(Q(j,n)+S((j-1),n)))
G(j,n) = ((x(j,n)-x((j-1),n))*R(j,n)) + ((y(j,n)-y((j-1),n))*S(j,n))
H(j,n) = ((x(j,n)-x((j-1),n))*(U(j,n)-U((j-1),n))) + ((y(j,n)-y((j-1),n))*(V(j,n)-V((j-1),n)))
end

3 件のコメント

Torsten
Torsten 2022 年 11 月 29 日
We don't know the sizes of the arrays on the right-hand side of your equations. So we cannot tell if this is the reason for a possible access violation.
Did you do the same mistake for these arrays as for E (you allocate it as 1x1000, but fill it as if it were 9x1) ?
Jan
Jan 2022 年 11 月 29 日
Please post the complete error message, which teels you, in which line the code is failing. It is hard to guess this detail.
Avoid an overdoing of the perantheses. Compare:
H(j,n) = ((x(j,n)-x((j-1),n))*(U(j,n)-U((j-1),n))) + ((y(j,n)-y((j-1),n))*(V(j,n)-V((j-1),n)))
H(j,n) = (x(j,n) - x(j-1,n)) * (U(j,n) - U(j-1,n)) + (y(j,n) - y(j-1,n)) * (V(j,n) - V(j-1,n))
Dyuman Joshi
Dyuman Joshi 2022 年 11 月 29 日
Please show the full error. It would be also helpful if you mention the sizes of all the arrays used in code.

サインインしてコメントする。

回答 (1 件)

Srija Kethiri
Srija Kethiri 2022 年 12 月 15 日

1 投票

Hi Shubham,
I understand that you are getting an error “Index in position 1 exceeds array bounds” when you run your code.
The reason for the error is:
  • You have declared the variable “E” as an array of size [1,1000] i.e., “E” has one row and 1000 columns.
  • And in the for loop, you are trying to enter the value in E(2,1). As “E” has only one row and you are trying to access the element in the 2nd row 1st column, it is throwing the error.
To know more about arrays and accessing the elements in arrays you can refer to the following documentation:
Hope this helps!

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

質問済み:

2022 年 11 月 29 日

回答済み:

2022 年 12 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by