MathGroup Archive 2005

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

Search the Archive

Re: Position function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55592] Re: Position function
  • From: dh <dh at metrohm.ch>
  • Date: Wed, 30 Mar 2005 03:22:23 -0500 (EST)
  • References: <d2b4q9$77c$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hello Konstatin,

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}}

Position does just that. You only have to arrange the output to your 
taste, what can be done by Cases:
a = {{1, 3, 1, 2, 1}, {5, 6, 1, 1, 3, 1}};
p = Position[a, 1];
Cases[p, {1, x_} -> x]
gives:
{1, 3, 5}

or all sublist in one go, using Map and a pure function:
Flatten @ Position [#, 1] & /@ a
gives:
{{1, 3, 5}, {3, 4, 6}}
> 
> 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}

t={{1, 1}, {1, 3}, {1, 5}, {2, 3}, {2, 4}, {2, 6}};
t[[All,2]]
gives:
{1, 3, 5, 3, 4, 6}

Sincerely, Daniel


  • Prev by Date: Re: Position function
  • Next by Date: Re : Position function
  • Previous by thread: Re: Position function
  • Next by thread: Re: Position function