How can I multiply the non-zero elements of a SparseArray?
Example:
a = SparseArray[{1 -> 1, 3 -> 2, 7 -> 3}]
Times @@ a (* this returns 0, but I need 6! *)
How can I multiply the non-zero elements of a SparseArray?
Example:
a = SparseArray[{1 -> 1, 3 -> 2, 7 -> 3}]
Times @@ a (* this returns 0, but I need 6! *)
Please note the following:
The sparse array a is:
{1, 0, 2, 0, 0, 0, 3}
hence applying Times to this yields zero
You can see the underlying array using Normal
a // Normal
You could multiply the non-zero elements of a sparse array object:
Times @@ a["NonzeroValues"]
"NonzeroValues" is mentioned?
– Danvil
Feb 06 '14 at 13:42
SparseArray Properties with: SparseArray[{1}]["Properties"]. I gathered links to some of my uses of these in this answer.
– Mr.Wizard
Feb 06 '14 at 14:12