0

I'm working on a problem where I have a large system of particles and I want to choose the masses of those that meet two conditions: they have to be inside a circle with given radius and have to be of a given type (let's say, type 2).

I'm trying to do this through this line of code:

mass = {'mass' + str(i): mass[i,r_length[i,:]<=10 and p_type[i,:]==2] for i in range(0,number_of_snapshots)}

But what I got was the error

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

I don't understand why, because both r_length and p_type are arrays with same shape, and my idea is that, when the conditions for both of them are true, then the overall condition is true and then that element is selected. There is some problem with the function "and" for arrays?

1 Answer 1

1

Use the bitwise and (&) instead of the Boolean and (and), and use parenthesis for each condition:

mass[i, (r_length[i, :] <= 10) & (p_type[i, :] == 2)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.