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

Question:

I am trying to use a mask to mark the elements in the vector nodes that are in the matrix e. For that I am using a mask for the vector nodes, and trying to set to false the corresponding position, so that at the end I can extract all elements in nodes that are left with a TRUE in the corresponding mask array

def SortedInteriorNodes(p,e,t):
    # e is a matrix with two rows, p a matrix with 2 rows, t a matrix with 3 rows
    nn=p.shape[1]  # number of nodes
    nt=t.shape[1]  # number of triangles
    ne=e.shape[1]  # number of edges = number of boundary nodes
    nodes = np.arange(start=0, stop=nn) # 0 1 2 3 ....np-1
    mask = np.full((1, nn), True)
    for col in range(ne):
        for row in range(2):
            k = e[row, col]
            if (mask[k] == True):
                mask[e[row,col]]= False
    # I end up with a vector of boolean where the true indicate the position of Interior Nodes
    InteriorNodes = nodes[mask]
    return InteriorNodes

I get this error "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"in the line "if (mask[k] == True):"

I ran the debugger and saw that mask is an array of booleans so mask[k] is a single value not an array so I can’t make sense of the error, why would I have to use any or all if I am working with one element at a time not with an array? Any help will be appreciated

Asked By: J.C.VegaO

||

Answers:

This code will work, provided that your matrix ‘e’ only has integers and all integers are nonnegative and less than nn:

def SortedInteriorNodes(p,e,t):
    # e is a matrix with two rows, p a matrix with 2 rows, t a matrix with 3 rows
    nn=p.shape[1]  # number of nodes
    nt=t.shape[1]  # number of triangles
    ne=e.shape[1]  # number of edges = number of boundary nodes
    nodes = np.arange(start=0, stop=nn) # 0 1 2 3 ....np-1
    mask = [True] * nn
    for col in range(ne):
        for row in range(2):
            k = e[row, col]
            if (mask[k] == True):
                mask[e[row,col]]= False
    # I end up with a vector of boolean where the true indicate the position of Interior Nodes
    InteriorNodes = nodes[mask]
    return InteriorNodes

I changed mask = np.full((1, nn), True) to mask = [True] * nn.
The reason for the error is that you gave the mask variable two dimensions, (1 row with nn columns) rather than 1 dimension. In other words, if nn is 3, then you should have made a mask = [True, True, True] and instead you made it [[True, True, True]]. So when you ask if mask[0] == True you are asking it if the first element of mask is equal to True. If mask = [True, True, True] like I made it, the first element is True, which is what you wanted. But if mask = [[True, True, True]] like you made it, then the first element is the first row: [True, True, True], which contains three terms. This confuses your program, because the == operator compares two elements, and you just tried to compare a list of THREE elements to True, which is not allowed.

Answered By: Amish
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.