From: S. B. Gray on 8 Jul 2010 03:13 I have lists such as th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501, 462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} and I want to find the position of the last nonzero element (e.g. 3 at position 25). Trivial, but some poking around did not reveal a neat way. Anyone? Thank you. Steve Gray
From: Fred Simons on 8 Jul 2010 06:50 Op 08-07-2010 09:13, S. B. Gray schreef: > I have lists such as > > th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501, > 462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} > > and I want to find the position of the last nonzero element (e.g. 3 at > position 25). Trivial, but some poking around did not reveal a neat way. > > Anyone? Thank you. > > Steve Gray > > > Length[th] - LengthWhile[Reverse[th], # == 0 &] Regards, Fred Simons Eindhoven University of Technology
From: David Park on 8 Jul 2010 06:50 Position[th, Except[0], {1}] // Last {25} David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: S. B. Gray [mailto:stevebg(a)ROADRUNNER.COM] I have lists such as th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501, 462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} and I want to find the position of the last nonzero element (e.g. 3 at position 25). Trivial, but some poking around did not reveal a neat way. Anyone? Thank you. Steve Gray
From: Bob Hanlon on 8 Jul 2010 07:41 th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501, 462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; th /. {a___, 0 ..} :> {a} // Length 25 th /. {a___, 0 ..} :> Length[{a}] 25 Bob Hanlon ---- "S. B. Gray" <stevebg(a)ROADRUNNER.COM> wrote: ============= I have lists such as th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501, 462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} and I want to find the position of the last nonzero element (e.g. 3 at position 25). Trivial, but some poking around did not reveal a neat way. Anyone? Thank you. Steve Gray
From: Carl K. Woll on 8 Jul 2010 20:35 On 7/8/2010 2:13 AM, S. B. Gray wrote: > I have lists such as > > th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501, > 462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, > 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} > > and I want to find the position of the last nonzero element (e.g. 3 at > position 25). Trivial, but some poking around did not reveal a neat way. > > Anyone? Thank you. > > Steve Gray > > One can use SparseArray to do this: In[46]:= SparseArray[th] /. SparseArray[_, _, _, {_, {_, pos_}, _}] :> pos[[-1, 1]] Out[46]= 25 Carl Woll Wolfram Research
|
Next
|
Last
Pages: 1 2 Prev: replacement x->y except in Exp[x] Next: Pattern: x_List with conditions on elements |