Showing results for 
Search instead for 
Did you mean: 

How to compare consecutive groups of numbers in arrays

Go to solution



I require to place the solution into a 1D array.

I need to compare two arrays and save the same numbers that are in both arrays but also save groups of numbers that may be in only one of the two arrays being compared.


In the example l have not been able to place all consecutive groups of numbers 8,9,10,11,12 into a 1D array.


In the example l need to filter out the zeros and the number four.

The numbers in both arrays are always in descending order however may not be in the order of index number of array. That is 15 = 15 index number

A consecutive group of numbers in this example is a group that increase by a value of 1. E.g.. 8,9,0,22,23,24 are two consecutive groups of numbers.


Thank  you

0 Kudos
Message 1 of 12

Let me see if I understand what you want.  It seems you have multiple conditions.


1. No zeros are in the output array.

2. Any non-zero value in both input arrays will be in the output array.

3. Any series of consecutive numbers in either input array will be in the output array.

4. You mention descending order but your examples and code are in ascending order.  Which do you want?



1. Are the input arrays sorted or known to be in ascending (or descending) order?

2. How many output arrays do you want? Your VI has several.

3. If the two input arrays have series of consecutive numbers which do not appear in both arrays but are consecutive to each other, do they appear in the output array as one series? Example Array has [2,3,4,0,0,8,17,25] and Array 2 has [0,0,5,6,7,10,14].  Is the output array [2,3,4,5,6,7,8] or something else?

4. How big is the largest array you will ever process?



0 Kudos
Message 2 of 12

Hi Johnsold


1. Both input arrays the data is in descending order

2. One output array. (The vi only has several to help you do better than what l have achieved)

3. Yes. Your output is correct

4. In the end the size of an image. Very large no limit set. The 1D arrays are part of other code which l do not want to side track you with.


Thanks Lynn

0 Kudos
Message 3 of 12

Hi Lynn


Sorry 1. Both input arrays are in ascending order. That is 1, 2 ,3 etc

0 Kudos
Message 4 of 12

This turned into a bit more of a challenge than I initially anticipated.


The attached VI shows one way to do parts of what you want.


The part at the lower left finds non-zero duplicates. I use Replace Array Subset as it is much more memory efficient than Insert into Array. I initialize the output array with the size of the smaller of the two input arrays because there can never be more duplicates than elements in the shorter array. After the loops the Array Subset eliminates any excess elements.


The part at the lower right finds consecutive elements in one array.  It is initialized to the size of the array being tested, because that is the size required if every element is successive to the previous element. The logic is described on the block diagram.


For the complete process you require the lower left part would need to be repeated for the other input array.  Then the three output arrays (Duplicates, Consecutive 1, and Consecutive 2) would need to be run through the duplicates loop (2 passes) with the logic reversed to remove duplicates. Thst will generate the desired output array.  Because much of the code will be used two or three times, it should be made into subVIs. These parts I leave for you.


After you get it working, take a careful look at the memory requirements. Since you will be working with images, your arrays may get quite large. Look at the white paper on Handling Large Data Sets on NI's web site.  I did not search for the exact title.  It has much good information on hanlding large arrays.



0 Kudos
Message 5 of 12

Hi Lynn


You have done a very good job. I gone on at work and preformed correctional code which is turning into a mountain.


When providing a example for help l have tried to make it simple as possible. So when changing numbers l found two cases where the code was not robust that is changing 0,4,0 to either 0,6,6 (does not pick up the two 6's) or 4,6,6 (picks up all three numbers instead of only the two 6's). Don't worry you have provided a new approach to this old problem of mind.


I have not used all the new LV 2012 for loop features as yet like the conditional stop on the for loop. Good to see that in use. I am still in the LV8.6.1 mind set.


When you started to solve this problem did you first work it out on paper like a math's problem?


At the moment at work l have approached the job l am doing differently because l do not know how much array code l require. The second reason l am approaching the job this way is l am to make my first LVOOP project and using in place element structure for the first time to handle memory by using is around each group of data types for each group of arrays.


I am happy to go away with your solved solution. I will hit the accept button after l have seen your reply


Thank you Lynn.



0 Kudos
Message 6 of 12

Hi Lynn


I just realized in this case 6,6 would not occur it would always jump up to 6,7. I was thinking of another array.


I will test the array at work and provide feedback




Lynn again

0 Kudos
Message 7 of 12

I am sure there may be border cases which are not handled correctly.  One is the case where a zero occurs between consecutive values. [4,5,0,0,6,...].  If this should return [4,5,6], I think my code would fail because it resets on detecting a zero.


If you have knowledge of your process which can be used to make it more robust or to set limits on the size of the output arrays, that should be added. I made the output arrays large enough to handle the worst case.  If you know for example that never more than 100 consecutive values will be in the data, those arrays could be initialized to size = 100.


I did not write out long arrays on paper but I did draw some flow charts which describe the same logic that I attempted to put into the comments at the bottom of the BD.  It would be nice if there were easy ways to document flow charts on a diagram.


Another array based approach which I considered for the consecutive values part was to add 1 to the array and then compare The original array A and the added array A' for duplicate values. This requires allocating space for a second copy of the original array, so I did not follow up on this method.


I did not consider either LVOOP or the in place structure but both could be investigated.



0 Kudos
Message 8 of 12

Hi Lynn


I did have a look at the consecutive array solution. I have looked and looked l thought l knew how it worked but after a number of separate evaluations with other code l have failed. I am just glad it works. With the consecutive array solution l tried today my own code but l lost either the first number of the last number of a consecutive group of numbers.


I may have confused you but in doing so you have provided a real different view which l am very grateful. You have viewed the consecutive number problem as just a single 1D array problem. By doing this you have opened my eyes to another way to do the job and a way to handle this type of data. However l am also comparing two arrays. For example l have to pick up say two equal values in two columns that the consecutive array does not detect and then correctly locate a value in the final array if the value is not in the final array.


What l have not told you is also l have to be able to identify groups of numbers in two 1D arrays. E.g... Array 1 has 1,2,9,10 next array has 9,10 the final array ends up 9,10 when l would like 0,9,10. When viewing numbers by rows later on l can then detect with a array length vi the length of the 1,2 groups of numbers over a large number of columns. If 9,10 jumps to the front it becomes a wrong length. This is actually my main problem which has caused me a large amount of extra programming. I have gone back to basics to see what l can do differently and you have provided the first clue with the consecutive array. I placed it in my main program today and it filtered a lot variable data which l could not remove without losing real data. The main data lost in your solution was the say 17 number in two columns which in an image is a horizontal line. I know what l have just provided you in this paragraph is all new information but it provides a background as to what l am up to. I am getting pixel data from a 2D array and comparing +1 index 1D array with a normal array. This way l can view the data of the next column to see if there is a pattern. I am actually trying to program an array as how humans view objects in images.


I will have another go at integrating your solution next week





0 Kudos
Message 9 of 12

Hi Lynn


I have just stopped programming. I still have to finish what l stated in my last message but it is nearly complete. I am going bush for the next 4 days to cool off a little. It's been 30 +C for a number of days now.


  • When you will see the code l have developed around your beautiful consecutive array solution l will need some help to reduce the size of the code. I have already 7 extra for loops. Compacting code is not my best feature.


I will send the vi next week.





0 Kudos
Message 10 of 12