I'm trying to use a
SparseArray for some large homological algebra calculations. I'd like to find indices of a
SparseArray whose entries are equal to 1. If I were operating naively on the
ArrayRules version of the array, I'd just need do something like
Cases[sparserules, HoldPattern[_List -> 1]] and that would give me what I want. (FWIW: here's why you need the HoldPattern.)
If it helps, I am also happy with just being able to pull off one such element at a time -
Cases[sparserules, HoldPattern[_List -> 1], All, 1] does what I want on
ArrayRules output. The order is not important - if this returns a random entry that's equal to 1, that's also fine by me.
Unfortunately, this doesn't seem to work on
SparseArray objects, and I don't want to be packing and unpacking
SparseArrays all the time. Is there an efficient way to get all (or just one) indices of a
SparseArray whose entries are equal to a given element?