5

With some numpy array a, what I'd like to do is

indices = np.where((a < 4) or (a > 12))

This isn't valid. It just returns "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()". But this expression isn't ambiguous, and any and all don't do what I want to do. (any and all can't take compound expressions either. But if can. Confused...)

user2162806
  • 123
  • 1
  • 1
  • 6
  • 4
    `indices = np.where((a < 4) | (a > 12))` Aside: this is a duplicate. – mechanical_meat Mar 12 '13 at 22:51
  • Sorry for the duplication; I couldn't find any related article. Apparently because this has nothing to do with `np.where()`, but rather the fact that Python has both `or` and `|` and they do different things. Oh, Python, you silly beast... – user2162806 Mar 12 '13 at 22:58
  • No problem. It is oft-times difficult to find duplicates. Those two operators do different things. – mechanical_meat Mar 12 '13 at 23:00
  • They are not the same operator... – YXD Mar 12 '13 at 23:00
  • Yes, hence my comment about them doing different things. Does Python finish the craziness and have both `and` and `&` too? EDIT: Yes, yes, it does. *sigh* – user2162806 Mar 12 '13 at 23:06

1 Answers1

12

You want to get a logical/boolean array as your argument for where

You can do x | y or np.logical_or(x,y) , where x and y are a < 4 and a > 12

YXD
  • 31,741
  • 15
  • 75
  • 115