6 replaced http://stackoverflow.com/ with https://stackoverflow.com/
source | link

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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/
source | link

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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
source | link

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

Mathematica graphicsMathematica graphics

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

Mathematica graphicsMathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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
3 Fixed code
source | link
2 added 55 characters in body
source | link
    Post Deleted by Michael E2
1
source | link