user541686
user541686

Reputation: 210465

How many indices are allowed for a Matlab matrix?

I'm learning Matlab (actually, Octave) and something is really confusing me:

octave:14> a = [2 3 4]
                          a =

                             2   3   4

octave:15> a(1)
                          ans =  2
octave:16> a(1,1)
                          ans =  2
octave:17> a(1,1,1)
                          ans =  2
octave:18> a(1,1,2)
    error: A(I,J,...): index to dimension 3 out of bounds; value 2 out of bound 1

octave:18> a(2,1,1)
    error: A(I,J,...): index to dimension 1 out of bounds; value 2 out of bound 1

I expected a(1, 1, 1) to be illegal, but that's confusing me... how many indices are allowed for a matrix?

What does it mean when I say a(1, 1, 1)?

Upvotes: 1

Views: 972

Answers (1)

Jonas
Jonas

Reputation: 74940

In an array, the first row, column, page, etc are always defined, as long as the array isn't empty.

So if

a = 3;

a(1) %# works
a(1,1) %# works
a(1,1,1) %# works
a(1,1,1,1) %# works

because the size of a is, theoretically, [1,1,1,1,1,1,....]

For convenience, the size of a scalar is given as [1,1], i.e. the other dimensions of length 1 are not mentioned.

Upvotes: 2

Related Questions