MathGroup Archive 2007

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

Search the Archive

Re: Problem with Position applied on 2D list?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72605] Re: [mg72570] Problem with Position applied on 2D list?
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Fri, 12 Jan 2007 05:05:34 -0500 (EST)
  • References: <200701110656.BAA21663@smc.vnet.net>


On 11 Jan 2007, at 06:56, 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
>

The difference is actually as much a question of logic as of the way  
Position works. Note that Position, without explicit level  
specification, will look at all levels of your expression. Now, there  
is a difference between what happens when you want to find all  
positions of sub-expressions that contain 5 and those that do not  
contain 5. The only sub-expressions for which Member[#,5]& is True  
are sublists of your original list, so you get the expected result.  
But all kinds of other sub-expressions satisfy Not[Member[#,5]&. So  
to get what you want you should use level specification in Position  
like this:

integerRandomTable = Table[Table[Random[Integer, 10], {10}], {10}];

Position[integerRandomTable, _?(MemberQ[#1, 5] & ),{1}]

{{1}, {2}, {4}, {5}, {7}, {10}}


Position[integerRandomTable, _?( !MemberQ[#1, 5] & ), {1}]


{{0}, {3}, {6}, {8}, {9}}

Of course, as you already know, the former case works without level  
specification but that is actually a kind of accident and in general  
it is safer to use level specification, unless you really want  
Mathematica to look at all sub-expressions of your expression.


Andrzej Kozlowski
Oxford, UK


  • Prev by Date: Re: Problem with Position applied on 2D list?
  • Next by Date: Re: from range restriction to domain restriction
  • Previous by thread: Problem with Position applied on 2D list?
  • Next by thread: If & NIntegrate