LabVIEW

cancel
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?

 

Help.

0 Kudos
Message 1 of 12
(4,064 Views)

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
Message 2 of 12
(4,054 Views)

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 12
(4,024 Views)

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
0 Kudos
Message 4 of 12
(3,995 Views)

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

0 Kudos
Message 5 of 12
(2,369 Views)
0 Kudos
Message 6 of 12
(2,368 Views)

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?

0 Kudos
Message 7 of 12
(2,333 Views)

@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...

 

altenbach_1-1606342899309.png

 

0 Kudos
Message 8 of 12
(2,325 Views)

@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)

 

altenbach_0-1606343517638.png

 

0 Kudos
Message 9 of 12
(2,318 Views)

@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)

 

altenbach_0-1606344690429.png

 

0 Kudos
Message 10 of 12
(2,312 Views)