MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: position of sequence of numbers in list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116215] Re: position of sequence of numbers in list
  • From: "Mr. Wizard" <gleam at flashmail.com>
  • Date: Sun, 6 Feb 2011 05:37:39 -0500 (EST)

>I just found another way that outperforms both of our solutions on a
>wide range of integer arrays I tested. It turns out that one can
>extract the position directly from SparseArray, without using
>ArrayRules. It took me some time to figure it out:
>
>extractPositionFromSparseArray[
>   HoldPattern[SparseArray[u___]]] := {u}[[4, 2, 2]]
>
>positionExtr[x_List, n_] :=
>  Flatten@extractPositionFromSparseArray[
>    SparseArray[1 - Unitize[x - n]]]
>
>Again, no unpacking.
>
>Best,
>Norbert

Oliver Ruebenkoenig recently gave an easier way to get these values 
in the thread "Fast matrices comparison" started January 25th, 2011.

SparseArray[{0, 1, 0, 0, 1, 0, 1, 1, 0, 0}]["NonzeroPositions"]

SparseArray[{1}]["Properties"]

Paul 




  • Prev by Date: Pattern matching
  • Next by Date: Re: Definite integral
  • Previous by thread: Re: Pattern matching
  • Next by thread: Re: position of sequence of numbers in list