3

How do I check if there is an element in an array where both axes [X, Y] match [drawx, drawy]?

I have a NumPy array:

#format: [X, Y]
wallxy = numpy.array([[0,1],[3,2],[4,6]])

and two other variables:

#attached to a counter that increases value each loop
drawx = 3
drawy = 2

I'm using the array as a set of positions [[0,1],[3,2],[4,6]] and I need to test if [drawx, drawy] (also representing a position) is in one of the positions on both the X and Y axis etc. drawx = 4 drawy = 6 returns true drawx = 3 drawy = 2 returns true drawx = 4 drawy = 2 returns false drawx = 2 drawy = 1 returns false

GCaldL
  • 87
  • 5
  • [`[drawx, drawy] in wallxy` is actually doing `(wallxy == [drawx, drawy]).any()`.](http://stackoverflow.com/a/30690604/2357112) It's really weird and completely useless. – user2357112 Jul 16 '16 at 00:41
  • What's your desired result, and what is it doing instead? It seems to work just fine for me; `[3, 2] in wallxy` is `True` while, say, `[2, 3] in wallxy` is `False`. Did you want it to return a list of `bool` types for each index? – Jeff Jul 16 '16 at 00:56
  • 1
    This is a crude suggestion, but how about loop through each row in `wallxy` and then use `numpy.array_equal([drawx, drawy], [row])` to determine if your pair exists in the array? You're wanting to check _pair_ or _row_ equality, i.e. both elements in, and shape of the pair/row are a match e.g. [0,1] == [0,1] = True (both shape and elements match), [0,1] == [1,0] = False and [0,1] == [0,1,2] = False. – Jonathon Ogden Jul 16 '16 at 00:56
  • Or this might be what you want. Use `wallxy.flatten()` first: http://stackoverflow.com/questions/8625351/check-if-two-items-are-in-a-list-in-a-particular-order – Jeff Jul 16 '16 at 01:08

2 Answers2

2

== will broadcast the comparison, so

wallxy = numpy.array([[0, 1],[3, 2][4, 6]])
z0 = numpy.array([3,2])
z1 = numpy.array([2,3])

(z0==wallxy).all(1).any()  # True
(z1==wallxy).all(1).any()  # False

Which is, I think, what you're looking for.

Printing out the intermediate steps will be useful to understanding and working out similar tasks:

z0 == wallxy     # checks which elements match
#  array([[False, False],
#         [ True,  True],
#         [False, False]], dtype=bool)

(z0==wallxy).all(1)   # checks whether all elements of axis 1 match
#  array([False,  True, False], dtype=bool)

(z0==wallxy).all(1).any()   # checks whether any axis 1 matches z0
#  True

If instead you used z0 = numpy.array([2,3]), then everything would be False.

tom10
  • 67,082
  • 10
  • 127
  • 137
  • I actually wanted to check if there is an element in the array where both axis match `drawx` and `drawy`... (I apologize my original question didn't communicate that very clearly) Thanks – GCaldL Jul 16 '16 at 13:30
  • @Pineapples: I guess I don't understand. To get more specific. Do you care about the order? Does just one need to match or both? Do you care about whether it's a row or column? Do you just want one of the two to match anywhere in the array? The most helpful would be to give examples of things you want to match and things you don't. – tom10 Jul 16 '16 at 14:09
  • I'm using the array as a set of positions [[0,1],[3,2],[4,6]] and I need to test if drawx and drawy (also representing a position) is in one of the positions on both the X and Y axis etc. `drawx = 4 drawy = 6` **returns true** `drawx = 3 drawy = 2` **returns true** `drawx = 4 drawy = 2` **returns false** `drawx = 2 drawy = 1` **returns false** – GCaldL Jul 16 '16 at 14:35
  • @Pineapples: Yes, if I understand you correctly, that's what my answer does. I've re-organized it a bit to make that more prominent. – tom10 Jul 16 '16 at 16:03
  • 1
    Thanks for clarifying your answer. Your method worked great:`z0 = numpy.array([drawx, drawy]) if (z0==wallxy).all(1).any():` I was just missing `.any()` on the end of the line. Once again thanks, I really appreciate it. – GCaldL Jul 17 '16 at 01:04
-1

numpy doesn't have but list does

[3,2] in wallxy.tolist()
True
karakfa
  • 66,216
  • 7
  • 41
  • 56