Re: Position function

*To*: mathgroup at smc.vnet.net*Subject*: [mg55587] Re: Position function*From*: Curt Fischer <tentrillion at gmail.NOSPAM.com>*Date*: Wed, 30 Mar 2005 03:21:50 -0500 (EST)*References*: <d2b4q9$77c$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

konstantpi at mail15.com wrote: > suppose i have a List: > a={{1,3,1,2,1},{5,6,1,1,3,1}} > how i could know the positions of "1" in every sublist , i could use Position[a > [[1]],1] and Position[a[[2]],1] > but i want a one formula to give me {{1,3,5},{3,4,6}} In[1]:= a = {{1, 2, 1, 2, 1}, {5, 6, 1, 1, 3, 1}} Out[1]= {{1, 2, 1, 2, 1}, {5, 6, 1, 1, 3, 1}} In[7]:= Flatten@Position[#,1]&/@a Out[7]= {{1, 3, 5}, {3, 4, 6}} This method creates a pure function Position[#,1] which takes any list and gives the position of the 1s. The /@ operator maps this function onto a, but only at the first level of the list. The Flatten is there to give you the desired output instead of {{{1},{3},{5} ... There are two ways I can think of for your second question. In[8]:= Flatten[%] Out[8]= {1, 3, 5, 3, 4, 6} The first is to just flatten the output from above. In[10]:= #[[2]] & /@ Position[a, 1] Out[10]= {1, 3, 5, 3, 4, 6} The second is to create a pure function #[[2]]& which takes the second part of its argument, and to map this function onto the full position list which is created by Position[a, 1]. -- curt fischer > > the second question: > using Position[a,1] will give: > {{1, 1}, {1, 3}, {1, 5}, {2, 3}, {2, 4}, {2, 6}} > in general how to extract only the second item from every sublist so the output > will be; > {1,3,5,3,4,6} > thanks > konst. >