# LabVIEW

Showing results for
Did you mean:

Solved!
Highlighted

# Extract just the unique rows from 2d array

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?

Message 1 of 18 (1,289 Views)

## Re: Extract just the unique rows from 2d array

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

Message 2 of 18 (1,280 Views)
Solution
Accepted by topic author Werty12
‎08-27-2015 06:09 PM

## Re: Extract just the unique rows from 2d array

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:

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 (1,265 Views)

## Re: Extract just the unique rows from 2d array

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.

Message 4 of 18 (1,241 Views)

## Re: Extract just the unique rows from 2d array

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

Message 5 of 18 (1,236 Views)

## Re: Extract just the unique rows from 2d array

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.

Message 6 of 18 (1,225 Views)

## Re: Extract just the unique rows from 2d array

[ Edited ]

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.

Message 7 of 18 (1,219 Views)

## Re: Extract just the unique rows from 2d array

[ Edited ]

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.

Message 8 of 18 (1,213 Views)

## Re: Extract just the unique rows from 2d array

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.

Message 9 of 18 (1,209 Views)

## Re: Extract just the unique rows from 2d array

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.

Message 10 of 18 (1,201 Views)