From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Array Sort, Sum, and Append

Solved!
Go to solution

Wracking my brain on this one. Here is what I am trying to do:

 

Example input array:

7 1 1 4

7 1 0 3

7 5 6 0

8 2 4 1

8 6 4 7

3 3 3 3

4 1 2 1

4 0 2 2

4 3 0 1

5 2 1 3

5 3 4 2

 

I would like to identify the duplicates in column 0 (which I have done) and then sum the array elements in columns 1-3 by associated rows (which I sort have done), and then display the new output array as shown below (which I can't figure out).

 

New output array:

7 7 7 7

8 8 8 8

3 3 3 3

4 4 4 4

5 5 5 5

 

Thanks in advance for any help! I attached the scratchpad vi I was working on.

0 Kudos
Message 1 of 12
(2,713 Views)

Put each row of data in a cluster that has as many elements as there are item in each row.

 

Build those clusters into an array.

 

Pass the array of clusters to a Sort 1D array. The output will be sorted based on the first element in the cluster. Any tie will be broken based on the second element in the cluster, similarly for duplicates in the second, etc.

 

Remove the first element of the of the array and push it into a shift-register of the for loop. Call that shift register "previous value and sums".

 

Index of that array (with the first element deleted) and compare the first element of the cluster and compare it to what is in the Shift Register. If the "value"s match, add the new numbers to what is in the shift register. If it does not match, append the the values and and sums that were in the SR to a 2-D array in yet another SR.

 

Use a case structure to control if you adding to the existing sums or appending to the 2D array.

 

When done, pull what was left over in the Value and Sums SR to the 2D array.

 

That should get you close.

 

Watch the code run in execution highlighting mode ( light bulb on) to see if  I missed anything.

 

Post a clean image of your code if you get confused.

 

Spoiler
I do not have a LV License so I can not look at code.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 2 of 12
(2,697 Views)

@Ben wrote:

 

Spoiler
I do not have a LV License so I can not look at code.

Fight the power, vote for this idea. (You inspired it.)

 

mcduff

Message 3 of 12
(2,691 Views)

Hi dmh,

 

The VI you attached isn't miles away, but I'd suggest you use a 2D array rather than a matrix (although you could still use Matrix Size on a 2D array if you really want rows and columns, not that you necessarily need them).

 

You'd be almost there if you appropriately connected your bottom Shift Register.

Just remember that there are two conditions on which you want to output a value - the first values do not match (in which case you output the previous summed value) and the last value of the loop (in which case, output the value after summing).

You can use a Select node to choose between these cases and an Equals comparison between "i"+1 and "N".

 

I won't post the solution right now, but in the version of code that I wrote based on what you had I only needed to add the comparison with i and N (plus the increment node), a "Not" and an "Or".


GCentral
0 Kudos
Message 4 of 12
(2,630 Views)

1. Use Index Array to get your first column.  You just have to wire up the second (column) index to get the entire column.

2. Use Array Subset with just a 1 wired to the column index.  By default, it will give you everything to the end of the array.

 

Those two will greatly simplify some of your code.

 

3. If you had LabVIEW 2019, I would say to use a Map to store your data.  It does the sorting for you.  This has the added benefit that you do not have to have consecutive values in the original data.  You could do something similar with Variant Attributes since you are using LabVIEW 2016.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 12
(2,584 Views)

I like this, supper clean, I'll have to see how to do the equivalent with Variant Attributes in LV2016. You're passing through the top array in the false case? 

0 Kudos
Message 6 of 12
(2,567 Views)
Solution
Accepted by topic author dmh2020

Since now we're posting solutions, here's the VI I made earlier (backsaved to 2015).

Array Sort Scratchpad.png

This, whilst perhaps not as clean, is I think easier to understand as a step from the uploaded code.

As I noted in my earlier post, you can avoid the use of the Matrix and several other operations can be simplified or cleaned-up.


GCentral
0 Kudos
Message 7 of 12
(2,561 Views)
Solution
Accepted by topic author dmh2020

@dmh2020 wrote:

I like this, supper clean, I'll have to see how to do the equivalent with Variant Attributes in LV2016. You're passing through the top array in the false case? 


The little confusion with the Maps, the Boolean flag is "key not found".  So the FALSE case is doing the addition.  The TRUE case is passing through the bottom array. 

 

If you go the Variant Attribute route, the Get Variant Attribute returns a TRUE if the key was found.  So the Boolean logic will be reversed.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 12
(2,550 Views)

Could attach your 2019 vi - I have upgraded to that version and would like to study your solution closer. I was able to use "cbutcher" solution solution, as it just added a bit more logic to mine. Sorry about the matrix', the scratchpad is to be used in a larger program that uses matrix'.

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

What I attached was a snippet.  So you can just download the png file and then drag it onto a blank VI.  The code will just poof into existence.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 10 of 12
(2,503 Views)