0

Consider a simple code to form a SparseArray:

Q = 5;
a = Table[i, {i, Q}];
SparseArray[{{j_} /; j > 2 -> a[[j]]}, {Q}]

Part::pspec: Part specification j is neither a machine-sized integer nor a list of machine-sized integers. >>

What is wrong with the rule? It seems I can not address any matrix element without getting such a warning. Sorry for such a minor question.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Yasha Gindikin
  • 458
  • 3
  • 7

1 Answers1

3

As Belisarius says in a comment, usingRuleDelayed (:>) instead ofRule (->) will fix your code.

Q = 5;
a = Table[i, {i, Q}];
SparseArray[{{j_} /; j > 2 :> a[[j]]}, {Q}]
SparseArray[<3>, {5}]
Normal[%]
{0, 0, 3, 4, 5}
m_goldberg
  • 107,779
  • 16
  • 103
  • 257