From: Mala Jozsef on
Hi, I would like to use SparseArray for large matrices and would like to use members of a list when giving values to the entries.Here is a simple example: l=={1};
SparseArray[{i_,i_}-> l[[i]],{1,1}]

Part::pspec: Part specification i is neither an integer nor a list of integers.
SparseArray[< 1 >,{1,1}]

In fact it works if there are at most three list in the definition. Is there a solution without error messages? Thanks, Jozsef

From: Albert Retey on
Am 12.04.2010 12:48, schrieb Mala Jozsef:
> Hi, I would like to use SparseArray for large matrices and would like
> to use members of a list when giving values to the entries.Here is a
> simple example: l=={1}; SparseArray[{i_,i_}-> l[[i]],{1,1}]
>
> Part::pspec: Part specification i is neither an integer nor a list of
> integers. SparseArray[< 1 >,{1,1}]
>
> In fact it works if there are at most three list in the definition.
> Is there a solution without error messages? Thanks, Jozsef


>From the documenation:

With rules Subscript[pos, i]:>Subscript[val, i] the Subscript[val, i]
are evaluated separately for each set of indices that match
Subscript[pos, i].

So using RuleDelayed (:>) instead of just Rule (->) will prevent
evaluation of l[[i]] with a symbolic value for i which is the reason for
the error message:


SparseArray[{i_, i_} :> l[[i]], {1, 1}]


hth,

albert