Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I multiply the non-zero elements of a SparseArray?


a = SparseArray[{1 -> 1, 3 -> 2, 7 -> 3}]
Times @@ a (* this returns 0, but I need 6! *)
share|improve this question
Related: – Mr.Wizard Feb 6 '14 at 14:14
up vote 5 down vote accepted

Please note the following:

  1. The sparse array a is:

     {1, 0, 2, 0, 0, 0, 3}

    hence applying Times to this yields zero

  2. You can see the underlying array using Normal

      a // Normal
  3. You could multiply the non-zero elements of a sparse array object:

     Times @@ a["NonzeroValues"]
share|improve this answer
This is good to know! Could you perhaps point me to the documentation where "NonzeroValues" is mentioned? – Danvil Feb 6 '14 at 13:42
@Danvil As far as I know it isn't documented. You can see the other SparseArray Properties with: SparseArray[{1}]["Properties"]. I gathered links to some of my uses of these in this answer. – Mr.Wizard Feb 6 '14 at 14:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.