20

I want to know is there any one line code to find position of first nonzero number without calculating position of all non zero numbers. For example,

A = {0, 0, 0, 1, -2, -1, 0};
First[Flatten[Position[A, _?(# != 0 &)], 1]]

will give me position of first non zero number. But first it calculate position of all non zero number. Is there any way to stop position function when it found first non zero number?

user64494
  • 26,149
  • 4
  • 27
  • 56
Vajira
  • 601
  • 5
  • 13

9 Answers9

19

You can specify number of object to return in Position. For example,

list = RandomInteger[{-5, 5}, 100000];
Position[list, _?(# != 0 &), 1, 1] // AbsoluteTiming

{0.003050, {{1}}}

Position[list, _?(# != 0 &)] // First // AbsoluteTiming

{0.138709, {1}}

DavidC
  • 16,724
  • 1
  • 42
  • 94
halmir
  • 15,082
  • 37
  • 53
9
SeedRandom[1];
LengthWhile[RandomInteger[{-5, 5}, 100000], # == 0 &] + 1
(* 1 *)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
9

We can use SparseArray. It computes all the positions, but it's a bit faster than Position, esp. on packed arrays, since Position unpacks a packed array.

First @ SparseArray[packedarray]["NonzeroPositions"]

or

First @ SparseArray[Developer`ToPackedArray @ unpackedlist]["NonzeroPositions"]

Timings on ten random lists:

SeedRandom[1];
n = 10;
list = RandomChoice[{0.9, 0.1} -> {0, 1}, {n, 100000}];
packedlist = Developer`ToPackedArray @ list;

Table[ First @ SparseArray[Developer`ToPackedArray @ list[[i]]]["NonzeroPositions"], {i, n}] // AbsoluteTiming Table[ Position[list[[i]], _?(# != 0 &), 1, 1], {i, n}] // AbsoluteTiming

Table[ First @ SparseArray[packedlist[[i]]]["NonzeroPositions"], {i, n}] // AbsoluteTiming Table[ Position[packedlist[[i]], _?(# != 0 &), 1, 1], {i, n}] // AbsoluteTiming

{0.007372, {{15}, {28}, {4}, {20}, {13}, {2}, {8}, {7}, {42}, {9}}}
{0.009183, {{{15}}, {{28}}, {{4}}, {{20}}, {{13}}, {{2}}, {{8}}, {{7}}, {{42}}, {{9}}}}

{0.005098, {{15}, {28}, {4}, {20}, {13}, {2}, {8}, {7}, {42}, {9}}}
{0.024295, {{{15}}, {{28}}, {{4}}, {{20}}, {{13}}, {{2}}, {{8}}, {{7}}, {{42}}, {{9}}}}


Compiled

It's rather straightforward in an imperative paradigm:

firstnzp = Compile[{{list, _Integer, 1}},
  Do[If[list[[i]] != 0, Return[i]], {i, Length@list}],
  RuntimeOptions -> "Speed"
  ]

And fast:

Table[firstnzp @ list[[i]], {i, n}] // AbsoluteTiming

{0.004164, {15, 28, 4, 20, 13, 2, 8, 7, 42, 9}}

Michael E2
  • 235,386
  • 17
  • 334
  • 747
8

While this certainly isn't a contender for speed, patterns are fun:

A /. {z : Longest[(0 | 0`) ...], ___} :> Length@{z} + 1
rm -rf
  • 88,781
  • 21
  • 293
  • 472
6

Just for fun:

ArrayRules[list][[1, 1, 1]]
user1066
  • 17,923
  • 3
  • 31
  • 49
4

Yet another compiled function:

firstnzp2 = Compile[{{list, _Integer, 1}}, 
  Module[{n = 0}, While[list[[++n]] == 0]; n], 
  CompilationTarget -> "C", RuntimeOptions -> "Speed"];

The speed is the same as Michael's compiled code after addition CompilationTarget -> "C".

Or one-liner:

1 + Total@UnitStep[-Accumulate@Unitize[list]]
ybeltukov
  • 43,673
  • 5
  • 108
  • 212
3

Comparing:

Position[s, e_ /; e != 0] // Trace

Position[s, e_ /; e != 0, 1, 1] // Trace

The number of matched positions to be found at certain levels can be specified, then Position, searching left to right, stops as soon as it finds that many.

BoLe
  • 5,819
  • 15
  • 33
2

If you are interested in manual solution, this might be helpful as well.

    f[x_] := (If[x > 0, Return[Position[A, x][[1, 1]]]]; x)

sparr = SparseArray[{{1, 400} -> 2, {1, 90000} -> 2}]
A = Normal[sparr][[1]];
  Catch[Table[If[f[A[[i]]] > 0, Throw[f[A[[i]]]], f[A[[i]]]], {i, Length[A]}]]

400 (Timing 0.06 Sec)

sparr = SparseArray[{{1, 90000} -> 2}]
A = Normal[sparr][[1]];
  Catch[Table[If[f[A[[i]]] > 0, Throw[f[A[[i]]]], f[A[[i]]]], {i, Length[A]}]]

90000 (1.11 Sec)

Pankaj Sejwal
  • 2,063
  • 14
  • 23
1

Here is a simpler method to get the position of the first nonzero element:

Position[A, DeleteCases[A, 0][[1]]][[1, 1]]  (* Single Line *)

Here is how it works:

firstNonZeroElement =  DeleteCases[A, 0][[1]] (* Returns value of first non-zero element *)
Position[A, firstNonZeroElement] (* Finds positions of all occurances of firstNonZeroElement *)
First[Position[A, firstNonZeroElement]][[1]] (* Selects the first one *)
Astor Florida
  • 1,326
  • 7
  • 21