02-21-2020 04:26 PM
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.
Solved! Go to Solution.
02-21-2020 04:42 PM
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.
Ben
02-21-2020 04:53 PM
02-22-2020 08:54 AM
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".
02-24-2020 07:28 AM - edited 02-24-2020 07:29 AM
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.
02-24-2020 09:04 AM
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?
02-24-2020 09:23 AM
Since now we're posting solutions, here's the VI I made earlier (backsaved to 2015).
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.
02-24-2020 10:17 AM
@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.
02-24-2020 04:01 PM
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'.
02-24-2020 06:50 PM
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.