Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
"Unique" row comparison with duplicate row counter

Subject: "Unique" row comparison with duplicate row counter

From: Josh

Date: 9 Sep, 2010 22:19:04

Message: 1 of 4

Hi Everyone,

     I have a large sorted array (More than 100,000 rows), comprized of three columns. The first two columns represent X and Y coordinates, and the third column is a counter used to keep track of duplicate points.

What I want to do, is keep only unique rows (the "unique" command is likely to come in handy). However, I only want to compare the first two columns for uniqueness, remove any duplicates, and increase column 3 by the number of duplicates that I have just removed (Note: previous accumulation of duplicates may be present in the top sorted position of column 3).

For example, if I have the following Array:

A=
[ 3 7 2
  3 7 1
  3 1 1
  2 6 1
  2 -5 3
  2 -5 1
  2 -5 1
 -1 9 1
 -1 9 1
 -1 -9 1]

The result should be:
[ 3 7 3
  3 1 1
  2 6 1
  2 -5 5
 -1 9 2
 -1 -9 1]

I recognize that it is probably easier to seperate the first two columns from the counter (3rd column) for uniqueness comparison, which is fine as long as they are indexed correctly.

If anyone has a quick method for doing this, It would be greatly appreciated.

Thanks,
     Josh

Subject: "Unique" row comparison with duplicate row counter

From: ImageAnalyst

Date: 9 Sep, 2010 22:22:39

Message: 2 of 4

Are the first two columns guaranteed to be integers?

Subject: "Unique" row comparison with duplicate row counter

From: Roger Stafford

Date: 10 Sep, 2010 00:27:22

Message: 3 of 4

"Josh " <joshinbox@hotmail.com> wrote in message <i6bmgo$o8e$1@fred.mathworks.com>...
> Hi Everyone,
>
> I have a large sorted array (More than 100,000 rows), comprized of three columns. The first two columns represent X and Y coordinates, and the third column is a counter used to keep track of duplicate points.
>
> What I want to do, is keep only unique rows (the "unique" command is likely to come in handy). However, I only want to compare the first two columns for uniqueness, remove any duplicates, and increase column 3 by the number of duplicates that I have just removed (Note: previous accumulation of duplicates may be present in the top sorted position of column 3).
>
> For example, if I have the following Array:
>
> A=
> [ 3 7 2
> 3 7 1
> 3 1 1
> 2 6 1
> 2 -5 3
> 2 -5 1
> 2 -5 1
> -1 9 1
> -1 9 1
> -1 -9 1]
>
> The result should be:
> [ 3 7 3
> 3 1 1
> 2 6 1
> 2 -5 5
> -1 9 2
> -1 -9 1]
>
> I recognize that it is probably easier to seperate the first two columns from the counter (3rd column) for uniqueness comparison, which is fine as long as they are indexed correctly.
>
> If anyone has a quick method for doing this, It would be greatly appreciated.
>
> Thanks,
> Josh
- - - - - - - - -
  Since we have your assurance that A is already in sorted order (presumably in its first two columns,) it would perhaps be a waste of cpu time to sort it again with the unique function. Try this:

 f = find([true;diff(A(:,1))~=0|diff(A(:,2))~=0;true]);
 p = f(1:end-1);
 B = A(p,:);
 B(:,3) = B(:,3)+f(2:end)-p-1;
 
Roger Stafford

Subject: "Unique" row comparison with duplicate row counter

From: Roger Stafford

Date: 10 Sep, 2010 01:01:23

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i6bu1a$mlr$1@fred.mathworks.com>...
> Since we have your assurance that A is already in sorted order (presumably in its first two columns,) it would perhaps be a waste of cpu time to sort it again with the unique function. Try this:
>
> f = find([true;diff(A(:,1))~=0|diff(A(:,2))~=0;true]);
> p = f(1:end-1);
> B = A(p,:);
> B(:,3) = B(:,3)+f(2:end)-p-1;
>
> Roger Stafford
- - - - - - - - -
  I could have eliminated one line of code by writing:

 f = find([true;diff(A(:,1))~=0|diff(A(:,2))~=0;true]);
 B = A(f(1:end-1),:);
 B(:,3) = B(:,3)+diff(f)-1;
 
Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us