0

I am trying to determine boundary node indices in a 2D domain using the following python code,

import numpy as np
...
b_index0 = np.where( (nodeDat[:,2] == xu) or
                     (nodeDat[:,2] == xl) or
                     (nodeDat[:,3] == yu) or
                     (nodeDat[:,3] == yl) )

where nodeDate[:,2] and nodeDat[:3] are lists of floating point x and y values respectively. This produces the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I tried a number of variations using np.add() and np.all(), but none seemed to work.

If I use | instead of or, I get the following error:

ValueError: could not broadcast input array from shape (200) into shape (1)

By experimentation, I found that the following code generates a correct list of 200 indices, which is the answer I require :

b_index00 = np.where(nodeDat[:,2] == xu)
b_index01 = np.where(nodeDat[:,2] == xl)    
b_index02 = np.where(nodeDat[:,3] == yu)
b_index03 = np.where(nodeDat[:,3] == yl)

b_index0 = np.unique(np.hstack((b_index00, b_index01,b_index02, b_index03)))

However, this seems to be an inefficient/inelegant way of achieving the desired result. It also produces duplicate indices, hence the call to np.unique().

The following suggested link provides some background as to why the initial code does not work, but does not appear to provide a solution to my particular problem. stackoverflow answer

Graham G
  • 551
  • 5
  • 14
  • 1
    use the `|` operator instead of `or` – James Jan 26 '20 at 12:41
  • Using the '|' instead of 'or' gives the following error: ValueError: could not broadcast input array from shape (200) into shape (1).I have also updated the question regarding np.any() and 'np.all()'. – Graham G Jan 26 '20 at 12:46
  • 2
    The `|` should work. The broadcast error suggests something funny or inconsistent about the `nodeDat` array. – hpaulj Jan 26 '20 at 14:45
  • Test the condition logic by itself, without the `where` call. Python evaluates that array first. where just finds the true elements. – hpaulj Jan 26 '20 at 14:53

0 Answers0