Error, bad index into Matrix - Maple Programming Help

 Description Indexing into a Matrix, Array, or Vector must be done with integer values. Related to this, multi-element selection with a Matrix, Array, or Vector must also come down to integers at the endpoints. Trying to index a Matrix with a symbol will result in an error.

Examples

Example 1

Note that $n$ has no value in this example.

 >
 ${A}{:=}\left[\begin{array}{rr}{1}& {2}\\ {3}& {4}\end{array}\right]$ (2.1)
 > $A\left[n,n\right]$

Solution:

In this case, defining corrects the error.

 > $n≔1$
 ${n}{:=}{1}$ (2.2)
 > $A\left[n,n\right]$
 ${1}$ (2.3)

Example 2

This one is more subtle because the sum command is following Maple's normal evaluation rules. The index operation $V\left[i\right]$ is attempted before sum is called.  At this point, the name $i$ does not have a value.

 >
 ${V}{:=}\left[\begin{array}{r}{1}\\ {2}\\ {3}\\ {4}\end{array}\right]$ (2.4)
 > $\mathrm{sum}\left(V\left[i\right],i=1..4\right)$

Solution 1:

Evaluation of needs to be delayed using right single quotes.

 >
 ${10}$ (2.5)

Solution 2:

Instead of sum, use add.  The add command is recommended for adding a finite sequence of values.  The add command has special evaluation rules, so no error occurs in the evaluation.

 >
 ${10}$ (2.6)

Example 3
A nested call to sum with only one set of right single quotes leads to a different error. A second set of right single quotes corrects the problem.

 >
 ${A}{:=}\left[\begin{array}{rrr}{2}& {3}& {4}\\ {3}& {4}& {5}\\ {4}& {5}& {6}\end{array}\right]$ (2.7)
 > $\mathrm{sum}\left(\mathrm{sum}\left(A\left[i,j\right],i=1..3\right),j=1..3\right);$
 > $\mathrm{sum}\left(\mathrm{sum}\left('A'\left[i,j\right],i=1..3\right),j=1..3\right);$

Solution:

 >
 ${36}$ (2.8)