How to check whether all elements of array are in between two values
The docs for np.nadarray.all
state:
Returns True if all elements evaluate to `True`.
The implication is that the method is generally used with Boolean arrays. If not used in this way, you may see all sorts of unintended consequences from that fact 0
is considered False
and 1
True:
np.arange(10).all() # False
np.ones(10).all() # True
np.zeros(10).all() # False
Comparison operators of a 1d array versus a number automatically produce Boolean arrays. You can also use the &
operator to combine multiple comparisons element-wise. So you can rewrite your logic:
def foo(rotation, speed):
avgs = rotation / speed
return ((avgs >= 55) & (avgs <= 55.2)).all()
Notice there's no need for an if
statement here. The result of the np.ndarray.all
is a Boolean value, i.e. either True
or False
.
Furthermore, a more succinct way of writing your logic is possible via np.isclose
and its atol
("absolute tolerance") argument:
def foo(rotation, speed):
avgs = rotation / speed
return np.isclose(A, 55.1, atol=0.1)
Check if array values are between two values of another array
You can use ufunc.outer
like so:
steps = np.arange(0, 22.5, 2.5)
vect = np.random.normal(10, 5.0, 10)
np.less.outer(vect,steps[1:]) & np.greater_equal.outer(vect,steps[:-1])
Sample output:
array([[False, False, False, False, False, False, True, False],
[False, False, False, False, True, False, False, False],
[False, True, False, False, False, False, False, False],
[False, False, False, True, False, False, False, False],
[False, False, False, True, False, False, False, False],
[False, False, False, False, True, False, False, False],
[False, False, False, False, False, False, False, True],
[False, False, False, True, False, False, False, False],
[False, False, True, False, False, False, False, False],
[False, False, False, True, False, False, False, False]])
Or as DataFrame
pd.DataFrame(np.less.outer(vect,steps[1:]) & np.greater_equal.outer(vect,steps[:-1]), columns=[f"{steps[i]:.1f}-{steps[i+1]:.1f}" for i in range(len(steps)-1)])
Output:
0.0-2.5 2.5-5.0 5.0-7.5 7.5-10.0 10.0-12.5 12.5-15.0 15.0-17.5 17.5-20.0
0 False False False False False False True False
1 False False False False True False False False
2 False True False False False False False False
3 False False False True False False False False
4 False False False True False False False False
5 False False False False True False False False
6 False False False False False False False True
7 False False False True False False False False
8 False False True False False False False False
9 False False False True False False False False
How to check if value is between two values stored in an array
This will return the higher index, for the next larger value. I have assumed from your example that the array is already sorted
let largerIndex = pressureLevels.firstIndex(where: { $0 > tempPressure})
let smallerIndex = largerIndex - 1
Note the edge cases, if there is no value larger than tempPressure
then largerIndex
will be nil
and if all values in the array are larger then largerIndex
will be 0
how to see if there is a value in a list that is between two values from another list
It really depends on what you want it to return. I wrote a code that will return the first pattern that it finds, but with some changes I'm sure it would not be difficult to return all combinations.
def get_between(a, b):
a, b = sorted(a), sorted(b)
for b_value in b:
smaller = None
greater = None
for a_value in a:
if b_value > a_value:
smaller = a_value
elif b_value < a_value:
greater = a_value
if smaller and greater:
return f"{b_value} is between {smaller} and {greater}"
return "There is no such combination"
a = [1, 4, 12]
b = [2, 13]
print(get_between(a, b))
The output on that case will be 2 is between 1 and 4
, but you can adapt the return value to be whatever you want.
Easy way to test if each element in an numpy array lies between two values?
One solution would be:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
(a > 1) & (a < 5)
# array([False, True, True, True, False])
results array between two values - javascript
A slightly different approach, with a customized callback function for filtering.
function filterNumbers(min, max) { return function (a) { return a >= min && a <= max; };}
var numbers = ['10', '15', '20', '25', '30', '35', '40', '45', '50'], result = numbers.filter(filterNumbers(10, 30));
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Find if numbers in an array are between two numbers and output number of values that are between
You could test, if the value is in the given range and count it.
var array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20], lower = 18, upper = 20, result = array.reduce(function (r, a) { return r + (a >= lower && a <= upper); }, 0);
console.log(result);
Check if all values of array are equal
const allEqual = arr => arr.every( v => v === arr[0] )
allEqual( [1,1,1,1] ) // true
Or one-liner:
[1,1,1,1].every( (val, i, arr) => val === arr[0] ) // true
Array.prototype.every (from MDN) :
The every()
method tests whether all elements in the array pass the test implemented by the provided function.
Related Topics
How to Update/Delete Rows in Bigquery from the Python API
How to Compare Two Image Files Contents in Python
Split Datetime Column into a Date and Time Python
Getting S3 Objects' Last Modified Datetimes With Boto
Selecting Specific Rows of CSV Based on a Column'S Value in Python
Cast String to Float Is Not Supported in Linear Model
Get Current Url from Browser Using Python
Tensorflow:Attributeerror: 'Module' Object Has No Attribute 'Mul'
How to Print Colored Text to the Terminal
How to Serialize Sqlalchemy Result to Json
How to Flatten a Hierarchical Index in Columns
How to Print Just the First Letters of Each Word
How to Get the Coordinates of the Bounding Box in Yolo Object Detection
Pandas: Sum Dataframe Rows for Given Columns
Python: How to Calculate the Average Word Length in a Sentence Using the .Split Command
Convert the String 2.90K to 2900 or 5.2M to 5200000 in Pandas Dataframe
Python Ssl.Sslerror: [Ssl: Certificate_Verify_Failed] Certificate Verify Failed (_Ssl.C:748)