From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I return the subset of an array that matches a condition/set of conditions?

Solved!
Go to solution

I've often run into this problem in LabVIEW, probably a result of my Matlab based thinking about arrays. Suppose I've got a 1-D array that consists of real numbers between 0 and 2 (e.g.):


1.3 0.1 0.7 1.9 1.4 1.5 1.5 0.8 1.3 0.3 1.4 1.4 0.1 0.6 0.1 0.2 1.6 1.6 1.4 0.6 1.9 0.1

 

and I want to know the mean of the subset of values that are greater than 0.5 and less than 1.2:

 

mean([0.7 0.8 0.6 0.6])  = 0.6750.

 

How can I return the subset of the array that meets the condition >0.5 & <1.2 ?

0 Kudos
Message 1 of 3
(3,068 Views)

Hi James,

 

you can:

1) use a for loop to put all interesting values into a new array and calc the mean from that

2) sort your 1d array, then search for the first element fitting min criteria, then search for max criteria, then you know the indices to cut out from the big array...

3) use a for loop and call "Mean PtByPt" in a case structure...

 

Many ways lead to Rome Smiley Wink

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 3
(3,066 Views)
Solution
Accepted by topic author jamespherman

For your example (values between 0.5 and 2) if you want this:

 

front panel.PNG

 

 

I would do something like this:

 

block diagram.PNG

 

I just generated an array of 10 random numbers between 0.0 and 2.0

The next loop sorts through the data, by using 'In Range and Coerce'.
It then outputs the data that is in the selected range.

 

The last step is to take the average

 

 

I attached the VI if you would like to use the code.

Message Edited by Cory K on 08-13-2009 09:21 PM
Cory K
Message 3 of 3
(3,041 Views)