Jan-Bert -5 years ago 137

Python Question

i have a set of 3 arrays which i would compare to eachother.

array a contain a set of values and the values of array b and c should be partly the same.

it is so that if let say

`a[0] == b[0]`

`c[0]`

for better explanation i try to show what i mean.

`import numpy as np`

a = np.array([2, 2, 3, 4])

b = np.array([1, 3, 3, 4])

c = np.array([2, 2, 4, 3])

print(a == b)

# return: [False False True True]

print(a == c)

# return: [True True False False]

as you can see the from both sets i have two True and two False. so if one of both is true the total should be true. when i do the following i get a single True/ False for an array. and the answers are what i want...

`print((a == b).all())`

# return: False

print((a == c).all())

# return: False

print(a.all() == (b.all() or c.all()))

# return: True

When i make the array b and c so that i have one vallue that in both cases is wrong i should end with a False

`import numpy as np`

a = np.array([2, 2, 3, 4])

b = np.array([1, 3, 3, 4])

c = np.array([5, 2, 4, 3])

print(a == b)

# return: [False False True True]

print(a == c)

# return: [False True False False]

print((a == b).all())

# return: False

print((a == c).all())

# return: False

So far so Good

`print(a.all() == (b.all() or c.all()))`

# return: True

this part is not good this should be a False!!

How do i get an or function like this so that i end with an True when for each value in

`a`

`b`

`c`

EDIT:

explanation about

`a[0] == b[0]`

`c[0]`

i have an python function where phase information comes in and have to do some actions.

before that i want check if i have to do with an array of imaginary values or with an array with phase angles. I want to check this before i do something. The problem is the phase angle because the right side is the inverted phase +/- pi. so for every value i have two options. and yes most of the time it is an exclusive or but in the case +/- pi/2 it is not since both are true and that is also fine...

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

From your example and explanation, I guess what you want is:

For each position,

exactlyone of`b`

or`c`

has the same value as`a`

If that's the case, that can be done with the following code:

```
def is_exclusively_jointly_same(a, b, c):
return np.logical_xor(a==b, a==c).all() # or use ^ below
# return ((a==b)^(a==c)).all()
```

The `^`

is exclusive or (XOR) operator, which returns `True`

if and only if exactly one of its argument is `True`

.

So the expression `(a==b)^(a==c)`

means either `a==b`

or `a==c`

, but not both. Then the `.all()`

checks whether this is true for all positions in the array.

Examples:

>>> a=np.array([1,2,3,4,5]) >>> b=np.array([1,2,0,0,5]) >>> c=np.array([0,0,3,4,0]) >>> is_exclusively_jointly_same(a, b, c) True >>> a=np.array([1,2,3,4,5]) >>> b=np.array([0,2,0,0,5]) # First value both not 1 >>> c=np.array([0,0,3,4,0]) >>> is_exclusively_jointly_same(a, b, c) False >>> a=np.array([1,2,3,4,5]) >>> b=np.array([1,2,0,0,5]) # First value both 1 >>> c=np.array([1,0,3,4,0]) >>> is_exclusively_jointly_same(a, b, c) False

If what you want is that:

For each position,

at leastone of`b`

and`c`

should have the same value as`a`

, then you need to change to OR instead of XOR, as follows:

```
def is_jointly_same(a, b, c):
return np.logical_or(a==b, a==c).all() # or use | below
# return ((a==b) | (a==c)).all()
```

Examples:

>>> a=np.array([1,2,3,4,5]) >>> b=np.array([1,2,0,0,5]) >>> c=np.array([0,0,3,4,0]) >>> is_jointly_same(a, b, c) True >>> a=np.array([1,2,3,4,5]) >>> b=np.array([0,2,0,0,5]) # First value both not 1 >>> c=np.array([0,0,3,4,0]) >>> is_jointly_same(a, b, c) False >>> a=np.array([1,2,3,4,5]) >>> b=np.array([1,2,0,0,5]) # First value both 1 >>> c=np.array([1,0,3,4,0]) >>> is_jointly_same(a, b, c) True

The key here is that `.all()`

should be applied *once* as final aggregator, when each individual values has already been calculated. So when you see that you are applying `.all()`

multiple times, you should be concerned.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**

Latest added