Showing results for 
Search instead for 
Did you mean: 

Sort 2D array and Add Same elements

I have a 2D array (see attached vi) and want to sort by column 0 (i can do that) and add elements of column 1 if there are similar elements in column 0


In the attached VI, I can sort the elements in the array based on column 0, but how do I add elements and display as Array out indicator?



0 Kudos
Message 1 of 4

You have a few problems.


  • Programs don't like terms like "similar". Lets assume they are either "equal" or "not equal" to simply things. 😄
  • It's hard to sum strings, so you need to convert to numeric and back. 


I no longer have LabVIEW 7.0, so a picture must suffice. Here's a quick draft.


In the first loop we create a 1D array containing a sorted list of all unique elements in column 0. In the second loop, we initialize an array with the sane number of elements as in the output from loop 1, then we index through the initial 2D array and find the element of column 0 to obtain its index, convert the approriate element from column 1 to a number, and add it to the corresponding element in the shift register. At the end we convert back to strings and form the output array.


This code will automatically scale to the number of different elements in column 0. It assumes that column 1 is all integers. If you have decimals, you would need to modify accordingly.


There are many other ways to do this. How big are the arrays? Do we need to worry about performance or memory?


Message Edited by altenbach on 05-19-2009 02:21 PM
Message Edited by altenbach on 05-19-2009 02:22 PM

LabVIEW Champion. It all comes together in GCentral GCentral
Message 2 of 4

Thanks altenbach. The code worked out great. Except, the array in have 9 columns. I only showed two in my test vi because they are the only ones used. Rest get sorted out or removed based on column 0. Your code only works for two columns (not your problem since I only showed two columns in my test vi).


How would that work?

0 Kudos
Message 3 of 4

I don't think your current specifications are sufficient to solve the problem because there are two possible interpretations.


  1. Do you want to create a single output column that contains the sum of all elements in matching columns?
  2. Do you want to create N output column, each summed seperately as in the example above?


In any case, both solutions would only need trivial modifications to the code above. In both cases you would replace the first "index array" in the second for loop with a size=1 index array to only get the first element and insert an "array subset" below it to get the remaining elements. Then you would either sum all elements and proceed as before or use a 2D array in the shift register to sum entire slices, depending on the specifications.


As an example, for case (2) you would e.g. do the following modification. It will automatically adapt to the number of columns.


(This is only a very quick draft, please verify correct operation and modify as needed) 😉


Message Edited by altenbach on 05-20-2009 08:45 AM

LabVIEW Champion. It all comes together in GCentral GCentral
0 Kudos
Message 4 of 4