6 replaced http://stackoverflow.com/ with https://stackoverflow.com/ edited May 23 '17 at 12:35 This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.WizardTimo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  5 replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/ edited Apr 13 '17 at 12:56 This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher'srasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher'srasher's Sign method is a bit faster than Positive. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  4 Added alternative solution edited Mar 13 '14 at 13:20 Michael E2 159k1313 gold badges218218 silver badges517517 bronze badges This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive:. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive: Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  This is reasonably fast and straightforward: SparseArray[Positive@S, Automatic, False]["NonzeroPositions"]  Update: Hmm, this slight modification of rasher's method is even faster on regular arrays: SparseArray[1 - UnitStep[-S]]["NonzeroPositions"]  Timing comparison. The array is about 90% zeros, 5% positive, 5% negative. Table[ foo = RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  If foo or S is already a SparseArray, then rasher's Sign method is a bit faster than Positive. Oddly, the "-foo" method is now slowest. Table[ foo = SparseArray[ RandomChoice[{0.9, 0.05, 0.05} -> {0, -2, 2}, {10^k, 100}]]; {SparseArray[UnitStep[Sign[foo] - 1]]["NonzeroPositions"] // timeAvg, SparseArray[Positive@foo, Automatic, False]["NonzeroPositions"] // timeAvg, SparseArray[1 - UnitStep[-foo]]["NonzeroPositions"] // timeAvg}}, {k, 3, 6} ] ~Prepend~ {"Sign", "Positive", "-foo"} // Grid  The timing function goes back to stackoverflow and Timo and Mr.Wizard. SetAttributes[timeAvg, HoldFirst] timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ AbsoluteTiming@Do[func, {5^i}], {i, 0, 15}];  Post Undeleted by Michael E2 occurred Mar 13 '14 at 12:47 3 Fixed code edited Mar 13 '14 at 12:47 Michael E2 159k1313 gold badges218218 silver badges517517 bronze badges 2 added 55 characters in body edited Mar 13 '14 at 12:29 Michael E2 159k1313 gold badges218218 silver badges517517 bronze badges Post Deleted by Michael E2 occurred Mar 13 '14 at 12:11 1 answered Mar 13 '14 at 12:01 Michael E2 159k1313 gold badges218218 silver badges517517 bronze badges