# Python function to quickly compare values of a matrix?

## Question:

I’m sorry to be asking a basic question, but I’ve been working on a signal analysis project for which we need to assign a variable based on which quadrant a set of values (n-dimensional vector) lies in. As a biomedical engineer, I’ve been struggling to maybe find a more efficient or "prettier" solution. Currently, the way in which I’m working for a 3 dimensional vector is by doing multiple comparissons:

```
if (ondas[0]>0)&(ondas[1]>0)&(ondas[2]>0):
note=1
elif (ondas[0]>0)&(ondas[1]>0)&(ondas[2]<0):
note=2
elif (ondas[0]>0)&(ondas[1]<0)&(ondas[2]<0):
note=3
elif (ondas[0]<0)&(ondas[1]<0)&(ondas[2]<0):
note=4
elif (ondas[0]<0)&(ondas[1]>0)&(ondas[2]<0):
note=5
elif (ondas[0]<0)&(ondas[1]<0)&(ondas[2]>0):
note=6
elif (ondas[0]<0)&(ondas[1]>0)&(ondas[2]>0):
note=7
elif (ondas[0]>0)&(ondas[1]<0)&(ondas[2]>0):
note=8
else:
note=0
```

Where `ondas`

is my array with 3 values. This code works sufficiently well, but I wonder if there’s another way to tackle the problem. I’ve been working well enough with this solution, but I’m open to feedback on the issue.

## Answers:

There are many different ways to approach this problem. The easiest is probably to create a boolean value for each axis and store the respective quadrant for each configuration in a dictionary. This is basically the same as what you did, only shorter and clearer:

```
axis_to_quad = {(0, 0, 0): 1,
(0, 0, 1): 2,
(0, 1, 1): 3,
(1, 1, 1): 4,
(1, 0, 1): 5,
(1, 1, 0): 6,
(1, 0, 0): 7,
(0, 1, 0): 8}
note = axis_to_quad[(ondas[0] < 0, ondas[1] < 0, ondas[2] < 0)]
```

Not really changing your logic, but replacing `&`

with `and`

, getting rid of lots of parens, adding some whitespace, and using a conditional expression.

```
note = 1 if ondas[0] > 0 and ondas[1] > 0 and ondas[2] > 0 else
2 if ondas[0] > 0 and ondas[1] > 0 and ondas[2] < 0 else
3 if ondas[0] > 0 and ondas[1] < 0 and ondas[2] < 0 else
4 if ondas[0] < 0 and ondas[1] < 0 and ondas[2] < 0 else
5 if ondas[0] < 0 and ondas[1] > 0 and ondas[2] < 0 else
6 if ondas[0] < 0 and ondas[1] < 0 and ondas[2] > 0 else
7 if ondas[0] < 0 and ondas[1] > 0 and ondas[2] > 0 else
8 if ondas[0] > 0 and ondas[1] < 0 and ondas[2] > 0 else
9
```