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?
You have a few problems.
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?
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?
I don't think your current specifications are sufficient to solve the problem because there are two possible interpretations.
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) 😉