MATLAB code error verification

11 Ansichten (letzte 30 Tage)
Chethana Unni P
Chethana Unni P am 25 Mai 2021
Beantwortet: Steven Lord am 25 Mai 2021
Hi
What does the error shown below implies??
Index in position 2 exceeds array bounds (must not exceed 1)

Antworten (2)

Jan
Jan am 25 Mai 2021
This means, that in the failing line of code an index is applied to the 2nd dimension of a variable, which has the size 1 only in this dimension:
x = (1:3).' % a column vector
x(1, 2) % No, the length of the 2nd dimension is 1 only

Steven Lord
Steven Lord am 25 Mai 2021
You're asking for an element of an array in a column that doesn't exist in that array. Consider a 4-by-4 matrix:
A = magic(4)
A = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
What's the element in the third row and fifth column of A? Since A doesn't have a 5th column, this question doesn't make sense and so MATLAB throws an error.
y = A(3, 5)
Index in position 2 exceeds array bounds (must not exceed 4).
In particular, because the error says that the index in position 2 must not exceed 1 we know that the array into which you're trying to index only has 1 column but you're asking for an element in columns 2, 3, or 4, or an even later column number.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by