MathGroup Archive 2007

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

Search the Archive

Re: Problem with Position applied on 2D list?


Heiko Damerau wrote:
> Dear All,
> 
>      There is some strange behaviour of the Position[] function I am 
> struggling with:
> 
> integerRandomTable = Table[Table[Random[Integer, 10], {10}], {10}];
> 
> Find the position of all lists containing the value 5 (just a simple 
> example):
> 
> Position[integerRandomTable, _?(MemberQ[#, 5] &)]
> 
> gives the expected result, e.g. {{1}, {3}, {4}, {5}, {6}, {7}, {8}}.
> However, just asking for all lists not containing the value 5
> 
> Position[integerRandomTable, _?(Not[MemberQ[#, 5]] &)]
> 
> Doesn't give the expected result but some strange thing, e.g. {{0}, 
> {1,0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, ..., {10, 3}, {10, 4}, {10, 5}, 
> {10, 6}, {10, 7}, {10, 8}, {10, 9}, {10, 10}, {10}, {}}
> 
>      What is the explanation for the different behaviour of MemberQ[] 
> and Not[MemberQ[]]?
> 
>      Thanks a lot in advance for any help.
> 
> Best regards,
> Heiko

Hi Heiko,

You must set the third parameter of the function Position to one to get 
the desired result. (Otherwise, Position applies the test to the whole 
hierarchy of nested levels and to the leaves.)

In[1]:=
integerRandomTable = Table[Random[Integer, 10], {10}, {10}]

Out[1]=
{{1, 3, 0, 4, 7, 4, 4, 2, 5, 2}, {1, 9, 8, 4, 9, 8, 3, 10, 2, 9},
   {8, 8, 8, 7, 5, 6, 3, 1, 8, 7}, {4, 3, 10, 5, 10, 6, 9, 3, 5, 1},
   {3, 5, 6, 0, 5, 0, 8, 9, 8, 7}, {2, 2, 8, 7, 8, 0, 5, 2, 6, 0},
   {6, 10, 9, 5, 1, 3, 9, 3, 0, 6}, {6, 2, 0, 4, 7, 9, 2, 6, 1, 7},
   {9, 1, 2, 0, 3, 8, 4, 2, 3, 5}, {5, 7, 1, 10, 9, 6, 3, 8, 9, 5}}

In[2]:=
Position[integerRandomTable, _?(MemberQ[#1, 5] & ), 1]
Position[integerRandomTable, _?( Not[MemberQ[#1, 5]] & ), 1]

Out[2]=
{{1}, {3}, {4}, {5}, {6}, {7}, {9}, {10}}

Out[3]=
{{0}, {2}, {8}}

Regards,
Jean-Marc


  • Prev by Date: Re: ListDimension function
  • Next by Date: Re: Problem with Position applied on 2D list?
  • Previous by thread: Re: question on Plot and Show plus Axis Labels
  • Next by thread: Re: Problem with Position applied on 2D list?