Message Boards Message Boards

2
|
5252 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Get positions of this list with UnsameQ and Infinity?

Posted 8 years ago

Why does the code below yield the positions given? In particular, why do {1,0}, {2,0} appear when these positions are not at level 2?

When UnsameQ is replaced by x neq infinity I get the correct result.

Thanks.

Position[({
   {?, 14, 0, 67, ?},
   {-3, ?, ?, 0, 6},
   {?, 0, ?, 66, 59},
   {45, ?, 5, ?, 0},
   {0, 29, 25, ?, ?}
  }), x_ /; UnsameQ[x, ?], {2}]

{{1, 0}, {1, 2}, {1, 3}, {1, 4}, {2, 0}, {2, 1}, {2, 4}, {2, 5}, {3, 
  0}, {3, 2}, {3, 4}, {3, 5}, {4, 0}, {4, 1}, {4, 3}, {4, 5}, {5, 
  0}, {5, 1}, {5, 2}, {5, 3}}
POSTED BY: Bruce Colletti
2 Replies

You can check what is happening by extracting the parts:

list = {
    {?, 14, 0, 67, ?},
    {-3, ?, ?, 0, 6}, 
    {?, 0, ?, 66, 59}, 
    {45, ?, 5, ?, 0}, 
    {0, 29, 25, ?, ?}}
Position[list, x_ /; UnsameQ[x, ?], {2}]
Extract[list, %]

which shows List elements. SameQ and UnSameQ ALWAYS give True or False, it can't remain unevaluated like UnEqual and Equal.

POSTED BY: Sander Huisman

Those positions are there because Position searches also the heads of expressions by default (you can turn that off with the option Heads -> False). When you have List[\[Infinity], 14, 0, 67, \[Infinity]], the head List is at levelĀ 2. Then UnsameQ[List, \[Infinity]] gives True, while Unequal[List, \[Infinity]] returns unevaluated.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract