LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating arrays in a nested for loop

Solved!
Go to solution

Hi all. 


I have been stuck in this problem for quite a while now and I still don't know how to continue. Some outside input would be greatly appreciated!! 

I'm trying to do the following: 

- Take an array of numbers, check if they are inside of a certain range (ex. between 2 and 4)

- Build two new arrays: one with all the numbers that are inside the range, and one with the rest. 

An additional condition is that the amount and value of range conditions will change (for example, it could be between 2 and 4 only/  2 and 4 AND 6 and 7 depending on the input)

To process this, I created two nested for loops - one that goes over an array that includes the range conditions and the inside one that actually goes and checks if the values are within the range. I think I've done this part successfully, but the next part is confusing to me - how do I actually create the separate arrays within the two for loops?


My apologies if I did not explain this well. Another method of conveying this problem is that I want to translate the following into Labview: 

Array ranges   // ex. [1 5 7 10] -> this means we want to split numbers based on those which are in (1,2) and (4,5) vs. those that aren't

Array values  // [2 3 6 11 3] 

Array inValues // Array for values inside range
Array outValues // Array for values outside range

 

for m = 1:size(ranges)

             for n = 1:size(values)
                           if (THE NUMBER IS INSIDE EITHER RANGE)
                                          inValues = [inValues  NEWNUMBER] 
                           else
                                         outValues = [outValues NEWNUMBER] 
                           end

              end
end

Seems easy enough, but it gets so chaotic with Labview that I don't know what to do. I've attached a picture for reference -  the insert into array function doesn't actually add into the array, it creates a new array. How can I save that up for when the nested for loop ends? 

 


I've seen some examples with shift registers that are within one loop and I could not successfully transfer that into my block diagram. 

Any help/direction would be greatly appreciated. 
 

0 Kudos
Message 1 of 7
(4,048 Views)
Solution
Accepted by topic author jylee55

 Hi!

 

See the following example to understand how you can create an array in For Loop 🙂

 

Arrays in for loop.png

In your case you can also uce Conditional terminal to create an array only when then numbers are within range. It makes the code much cleaner than case structures 🙂

 

Hope this helps, if not, let me know so I can help you!

 

Marcin

 

              

Think Dataflow - blog.mtapp.tech
0 Kudos
Message 2 of 7
(3,990 Views)

@jylee55 wrote:

Array ranges   // ex. [1 5 7 10] -> this means we want to split numbers based on those which are in (1,2) and (4,5) vs. those that aren't

 


A few comments.  First, I am totally mystified by your "Array ranges".  I see almost no relationship between the example numbers [1 5 7 10] and the resulting criteria, numbers in the range (1, 2) and (4, 5).

 

More important is the need to be very careful when talking about "in range", where you need to specify (carefully!) whether you are dealing with integers (which permit "equality" comparisons) or floats (which can "look the same" but fail to be "equal", for example, a representation of "0.1" and the result of 1 divided by 10).

 

A second is a "math thing" -- a convention is that writing [1, 2] means "numbers between 1 and 2, including the end points", while (1, 2) means "numbers between 1 and 2, excluding the end points".  So the integers in (1, 2) are ... the empty set, as there are no integers strictly between 1 and 2.

 

Finally, if you use the In Range function, note that you can choose to include or exclude either (or both) of the end points in your comparison, but be aware of whether you are using it for integers or floats.

 

Bob Schor

0 Kudos
Message 3 of 7
(3,967 Views)

Hi Bob, 

Sorry - [ 1 5 7 10] should lead to (1, 5) and (7, 10). 


I can understand your endpoint concerns from the 'ranges' example that I provided. However, I am not using this code for integers, but certain periods on a waveform. In my specific scenario there should not be any important signals at these end points other than noise. You are right and I should consider the endpoint scenarios more closely, but I want to fix this array problem I am having first. 

0 Kudos
Message 4 of 7
(3,953 Views)

@mtwar 

Hi! Thanks for your input, it works very well with finding an Array of numbers within that range. However, can I make it such that a second array is displayed which has the rest of the numbers, rather than displaying all of the numbers in that set? 

Thanks! 

0 Kudos
Message 5 of 7
(3,943 Views)
You can add one more conditional indexing and build array when the condition is false or true as required.
Thanks
uday
0 Kudos
Message 6 of 7
(3,939 Views)

@udka 

I see. I can just at a 'Not' and it will do the false ones. Thanks a lot guys!! 

0 Kudos
Message 7 of 7
(3,936 Views)