03-21-2025 04:54 PM
I created a small program to simulate a plot in my actual program. In my plot, I would like to know the max value (y-axis) in the time range I choose. In this sample program, I set the time range to every 0.5s (0, 0.5, 1, 1.5,....4). The actual x data I get has different time interval, so I circled it to make it clear. Between the time of interest between 0 and 0.5s , 2 data points (x = 0.1 and 0.4) fall in that range. From there, I find the max y value between 1.7 and 2.
I know I still have much to learn, so I was wondering if there's a better way to do the search and comparison.
.
03-22-2025 12:28 AM - edited 03-24-2025 10:24 AM
If you want help, then
03-22-2025 06:27 PM
Here's what I might do. Arguably simpler. Make sure you understand the principle!
(If there could be elements where x is below the first bounds element, some changes are needed and the output would need one more element)
If there is a range without elements (can happen, right?!), the max value is "-inf".(you could also use NaN, for example)
03-24-2025 08:27 AM
Wow, you're not kidding about fitting it in a postage stamp. I've never used In Place Element Structure. Definitely need to study this some more. Thanks for showing this to me!
03-24-2025 10:50 AM
The in place element structure is not critical here, and it basically just combines "index array" and "replace array subset/element" with slightly cleaner code. Both version operate efficiently "in place" on the output array, which gets allocated once and remains constant in size for the duration of the run. Each element contains the larges found so far, no memory allocations for subarrays, no array min&max, etc. needed.
More importantly, they xy arrays don't need to be sorted in x.
The more critical Function here is "threshold 1D array" which requires a sorted array of bounds and will return an interpolated index for each value in the x array. "Round to -Inf" ensures that the fractional index gets truncated (instead of rounded to the nearest index). A very powerful set of functions. Threshold function also accepts an array of points, useful for interpolation (example).