http://www.mathworks.com/matlabcentral/newsreader/view_thread/291277
MATLAB Central Newsreader  "Unique" row comparison with duplicate row counter
Feed for thread: "Unique" row comparison with duplicate row counter
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 09 Sep 2010 22:19:04 +0000
"Unique" row comparison with duplicate row counter
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291277#778330
Josh
Hi Everyone,<br>
<br>
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. <br>
<br>
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).<br>
<br>
For example, if I have the following Array:<br>
<br>
A=<br>
[ 3 7 2<br>
3 7 1<br>
3 1 1<br>
2 6 1<br>
2 5 3<br>
2 5 1<br>
2 5 1<br>
1 9 1<br>
1 9 1<br>
1 9 1]<br>
<br>
The result should be:<br>
[ 3 7 3<br>
3 1 1<br>
2 6 1<br>
2 5 5<br>
1 9 2<br>
1 9 1]<br>
<br>
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.<br>
<br>
If anyone has a quick method for doing this, It would be greatly appreciated.<br>
<br>
Thanks,<br>
Josh

Thu, 09 Sep 2010 22:22:39 +0000
Re: "Unique" row comparison with duplicate row counter
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291277#778333
ImageAnalyst
Are the first two columns guaranteed to be integers?

Fri, 10 Sep 2010 00:27:22 +0000
Re: "Unique" row comparison with duplicate row counter
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291277#778356
Roger Stafford
"Josh " <joshinbox@hotmail.com> wrote in message <i6bmgo$o8e$1@fred.mathworks.com>...<br>
> Hi Everyone,<br>
> <br>
> 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. <br>
> <br>
> 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).<br>
> <br>
> For example, if I have the following Array:<br>
> <br>
> A=<br>
> [ 3 7 2<br>
> 3 7 1<br>
> 3 1 1<br>
> 2 6 1<br>
> 2 5 3<br>
> 2 5 1<br>
> 2 5 1<br>
> 1 9 1<br>
> 1 9 1<br>
> 1 9 1]<br>
> <br>
> The result should be:<br>
> [ 3 7 3<br>
> 3 1 1<br>
> 2 6 1<br>
> 2 5 5<br>
> 1 9 2<br>
> 1 9 1]<br>
> <br>
> 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.<br>
> <br>
> If anyone has a quick method for doing this, It would be greatly appreciated.<br>
> <br>
> Thanks,<br>
> Josh<br>
        <br>
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:<br>
<br>
f = find([true;diff(A(:,1))~=0diff(A(:,2))~=0;true]);<br>
p = f(1:end1);<br>
B = A(p,:);<br>
B(:,3) = B(:,3)+f(2:end)p1;<br>
<br>
Roger Stafford

Fri, 10 Sep 2010 01:01:23 +0000
Re: "Unique" row comparison with duplicate row counter
http://www.mathworks.com/matlabcentral/newsreader/view_thread/291277#778360
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i6bu1a$mlr$1@fred.mathworks.com>...<br>
> 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:<br>
> <br>
> f = find([true;diff(A(:,1))~=0diff(A(:,2))~=0;true]);<br>
> p = f(1:end1);<br>
> B = A(p,:);<br>
> B(:,3) = B(:,3)+f(2:end)p1;<br>
> <br>
> Roger Stafford<br>
        <br>
I could have eliminated one line of code by writing:<br>
<br>
f = find([true;diff(A(:,1))~=0diff(A(:,2))~=0;true]);<br>
B = A(f(1:end1),:);<br>
B(:,3) = B(:,3)+diff(f)1;<br>
<br>
Roger Stafford