Find the closest smaller value of an array
If you array is sorted, and small enough, a really simple mode to do what you want it's simplly iterate over the array until number > number-in-array
then return the number on the previous position.
function getClosestValue(myArray, myValue){
//optional
var i = 0;
while(myArray[++i] < myValue);
return myArray[--i];
}
Regards.
Find nearest smaller number in array
Given that the array is sorted , You need
if let value = numbers.last(where: { $0 <= target }) {
print(value)
}
Infer nearest, smaller value in numpy array
Using searchsorted
is an option (as mentioned in the comment above and in the one of the answers in the linked question):
>>> true_values[-np.searchsorted(true_values[::-1], my_values)]
array([ 0.1, 1.2, 3. , 4.5, 0.1, 2.4, 1.2, 3. , 1.2])
Note that searchsorted
requires the true_values
to be sorted in ascending order. Here it's necessary to flip the order of your example array and then make the indices returned into negative integers for the fancy-indexing.
If true_values
is not sorted (in any direction), you'll need to use np.argsort
and the sorter
parameter in searchsorted
.
Find equal or nearest smaller value from an array
Sort the array and then do a binary search to find the values.
See:
https://en.wikipedia.org/wiki/Binary_search
and
Array.BinarySearch Method
Find nearest smaller value in Matlab
This can be done in a vectorized way as follows (note that the intermediate matrix d
can be large). If there is no number satisfying the condition the output is set to NaN
.
d = Triggs(:).'-Peaks(:); % matrix of pair-wise differences. Uses implicit expansion
d(d<=0) = NaN; % set negative differences to NaN, so they will be disregarded
[val, result] = min(d, [], 1); % for each column, get minimum value and its row index
result(isnan(val)) = NaN; % if minimum was NaN the index is not valid
If it is assured that there will always be a number satisfying the condition, the last line and the variable val
can be removed:
d = Triggs(:).'-Peaks(:); % matrix of pair-wise differences. Uses implicit expansion
d(d<=0) = NaN; % set negative differences to NaN, so they will be disregarded
[~, result] = min(d, [], 1); % for each column, get row index of minimum value
Related Topics
How to Handle Closure Recursivity
Make Int Round Off to Nearest Value
Segue from a Slpagingviewswift Vc and Dismiss the Destination Vc to Return
New @Convention(C) in Swift 2: How to Use It
Print() to Console Log with Color
Rxswift Map and Flatmap Difference
Add Instagram to Uiactivityviewcontroller
Rotate an Object in Its Direction of Motion
Diagnosing Exc_Bad_Instruction in Swift Standard Library
Scenekit Scnscenerendererdelegate - Renderer Function Not Called
Swift: How to Delete Part of Audio
How to Track More Than 4 Images at a Time with Arkit
What Is the Way to Save Fonts and Sizes in Firebase for Textview Swift
Swiftui Hide Tabbar in Subview
Differencebetween Type Safety and Type Inference
How to Include Assets/Resources in a Swift Package Manager Library