LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Extract just the unique rows from 2d array

Solved!
Go to solution

Hello, I'm sorry for my english. I have a 2D numeric array with thousends of rows and 2 columns. I want to extract just the unique rows from the array. So I need to exclude all the rows that/which(?) have duplicate(s) and exclude the duplicate(s) too.

 

Simple example:

 

Input array:


0 0 ---> Unique

0 1 ---> D1

1 0 ---> Unique

1 1 ---> D1

0 1 ---> D2

0 2 ---> Unique

1 1 ---> D2

2 0 ---> D1

1 1 ---> D3

2 0 ---> D2

2 2 ---> Unique

 

Desired output array:


0 0

1 0

0 2

2 2

 

Can anyone help me?

0 Kudos
Message 1 of 18
(5,925 Views)

I have no clue what makes the rows "unique".

0 Kudos
Message 2 of 18
(5,916 Views)
Solution
Accepted by Werty12

Hi Werty,

 

One way to do this is to compare each row with the entire array and count the number of matches.

If there is only one match, then the row is unique.

 

Here is an example:

Unique Rows.png

 

The outer For loop indexes through each row.

The inner For loop compares each row with the whole array and counts the matches.

Then a check is made so see if this row is unique.

The case statement adds unique rows to the output array.

 

Note: Using a Build Array function in a loop like this could cause problems if there are going to be a large number of unique rows.

--------------------------------------------------------------------------------------------------------------------------
Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
question. Give "Kudos" to replies that help.
--------------------------------------------------------------------------------------------------------------------------
Message 3 of 18
(5,901 Views)

Just for fun, let's make that solution a lot more efficient, by comparing each row only to the rows that have already been identified as unique instead of comparing them to every other row in the array.  I also decided here to maintain a list of the indices of the unique array rows instead of the row data.

Array Unique Rows.png

0 Kudos
Message 4 of 18
(5,877 Views)

OOPS!  I misunderstood the original intent of the question, my VI does not in fact solve the desired goal.  Sorry!

0 Kudos
Message 5 of 18
(5,872 Views)

Got too involved in making this work after my initial misunderstanding of the goal.  This version does what it's supposed to do and should be faster than the marked solution due to many fewer array comparisons, although it did come out more complicated than I'd hoped.  All false cases contain only a direct pass-through of the wires.

Array Unique Rows.png

0 Kudos
Message 6 of 18
(5,861 Views)

@Werty12 wrote:

Hello, I'm sorry for my english. I have a 2D numeric array with thousends of rows and 2 columns. I want to extract just the unique rows from the array. So I need to exclude all the rows that/which(?) have duplicate(s) and exclude the duplicate(s) too.


We need much better specifications than that:

 

  1. What is the datatype of the numbers?
  2. Is the order of the rows in the output important, and if so, what order should they be?
  3. What is your LabVIEW version?

 

Here's a quick solution assuming that the input array is U8. It can easily be adapted for any other datatype with a few small changes.

 

 

 

(I am guessing that the other solutions in this thread are much less efficient (O(N²) compared to my solution (O(NlogN)), so for large inputs array, the speed advantage of my solution will be very significant. My solution also operates mostly in-place, avoiding constant array resizing).

 

This is just a quick draft. I am sure it can be improved. 😉

Download All
Message 7 of 18
(5,854 Views)

stevem181 wrote:

One way to do this is to compare each row with the entire array and count the number of matches.

If there is only one match, then the row is unique.


  • I don't understand the purpose of the two "array subset" functions. The VI works the same if you delete them.
  • If you do the >1 comparison inside the inner loop, you can stop the loop early as soon as more than one instance is found.

Here's a slightly cleaned up version of your VI. (I don't recommend these solutions, because the won't scale well for large arrays)

 

 

 

As already mentioned, this version gets prohibitively expensive for large arrays because the number of comparisons grows with the square of the array size. This code is only useful for small problems.

 

0 Kudos
Message 8 of 18
(5,849 Views)

Altenbach - not surprisingly, your code is much faster than mine.  However, in the process of benchmarking, I'm seeing what is either a bug in your code or a deliberate design limitation based on the assumption of only 2 columns.  If your rows are greater than 2 columns wide, you don't necessarily get the right result.  Try feeding in this array:

0,0,0,1

0,0,0,2

0,0,0,3

Your code only returns the first two rows.

0 Kudos
Message 9 of 18
(5,845 Views)

@nathand wrote:

However, in the process of benchmarking, I'm seeing what is either a bug in your code or a deliberate design limitation based on the assumption of only 2 columns. 



I was going by the clear specifications in the original problem description:

 

Quote: "Hello, I'm sorry for my english. I have a 2D numeric array with thousends of rows and 2 columns. "

 

If the problem were posed differently, I would have posted different code. 😉

0 Kudos
Message 10 of 18
(5,837 Views)