LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Better way to search for the max value in an array range?

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.

Screenshot_2.png

.

Screenshot_1.png

0 Kudos
Message 1 of 5
(233 Views)

If you want help, then

 

  • Give your VI a normal and unique name. "untitled 1.vi" is an extremely poor choice.
  • Do a "save for previous", (2020 or below) before attaching, because many here don't have the newest LabVIEW versions.
  • Most likely all you need is a single FOR loop and take the max of a suitable subset
  • Search array requires binary identical values for a match and most likely will not work if this is real data. Very dangerous! (Not a problem here because you are finding an existing element, but important to know)
  • You don't need to wire the lower +1 index, because that is automatically one higher that the wired terminal above.
  • Is the "array" (I am sure there could be a better label name!) always sorted?
  • Done right, the code would probably fit on a postage stamp.
  • etc.
0 Kudos
Message 2 of 5
(200 Views)

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)

 

altenbach_2-1742685983348.png

 

altenbach_3-1742686033592.png

 

 

 

 

Message 3 of 5
(166 Views)

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!

0 Kudos
Message 4 of 5
(107 Views)

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).

0 Kudos
Message 5 of 5
(88 Views)