05-19-2009 03:44 PM
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?
Help.
05-19-2009 04:20 PM - edited 05-19-2009 04:22 PM
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?
05-20-2009 07:47 AM
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?
05-20-2009 10:43 AM - edited 05-20-2009 10:45 AM
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) 😉
11-24-2020 04:14 AM
Hi, I'm new in Labview. I have X-ray results and I want to sum all the element corrisponding to the same value. As example I have this 2D array
0.5 2
0.5 3
1 3
1.5 4
and as result I want this 2D array
0.5 5
1 3
1.5 4
I tried this VI but it doesn' work. Can I solve the problem? I hope you can help me!!! Thanks
11-24-2020 04:14 AM
I attach the VI
11-25-2020 03:53 PM - edited 11-25-2020 03:56 PM
I don't recommend adding to an ancient thread that is over 11 years old. Many things have changed.
Are you really using LabVIEW 2016? (In newer versions I would recommend an approach based on maps)
In any case, I am sure that all this can be done with code the size of half a postcard or less. You did not attach your datafile. What is the range and number of typical values in column 2. Are they always integers?
11-25-2020 04:21 PM - edited 11-25-2020 04:24 PM
@FFoglia wrote:
Hi, I'm new in Labview. I have X-ray results and I want to sum all the element corrisponding to the same value. As example I have this 2D array
0.5 2
0.5 3
1 3
1.5 4
and as result I want this 2D array
0.5 5
1 3
1.5 4
As you probably know, equal comparisons on DBL can be tricky, so be careful.
See if this works for you...
11-25-2020 04:34 PM - edited 11-25-2020 04:44 PM
@altenbach wrote:
... (In newer versions I would recommend an approach based on maps)
Here's how a map based solution would look like (LabVIEW 2019+). Arguably less and cleaner code. Same result.
(NOTE: In older versions, you could implement the same using variant attributes, but that's a bit more advanced)
11-25-2020 04:52 PM
@altenbach wrote:
(NOTE: In older versions, you could implement the same using variant attributes, but that's a bit more advanced)
Here's how that could look like.... (Still same result)