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:
finding duplicate entires and removing them

Subject: finding duplicate entires and removing them

From: Phanikumar Bhamidipati

Date: 30 Jan, 2009 22:46:01

Message: 1 of 6

Hi,
  I have an array of containing the two columns of data, first one is the index and the second one is the value. I want to find and remove any entires that have been repeated.

for eg.

a=[1 34
     2 33
     2 45
     3 32
     4 65
     4 99
     5 11
     1 33
     6 22];

I want the output to be

b=[3 32
     5 11
     6 22];

Please let me know any ideas.

Regards,

Subject: finding duplicate entires and removing them

From: Matt Fig

Date: 31 Jan, 2009 00:10:21

Message: 2 of 6

One method:

a=[1 34
     2 33
     2 45
     3 32
     4 65
     4 99
     5 11
     1 33
     6 22];

sa = sortrows(a,1);
[idx1,idx1] = unique(sa(:,1),'first');
[idx2,idx2] = unique(sa(:,1),'last');

newa = sa(idx1==idx2,:)





c)]\cf^\dgzhTcjj^5ozg`zpz"zjz\jb]oj\"o`hzd\tiqzpH;@ijk\\``k

Subject: finding duplicate entires and removing them

From: Phanikumar Bhamidipati

Date: 31 Jan, 2009 01:10:18

Message: 3 of 6

Hi Matt,
  Thanks for the suggestion. I found that with one correction, i got what i wanted. with your script, the output was

newa =
     2 33
     3 32
     4 65

instead of what i wanted. so i changed the last line of the code to

newa = sa(intersect(idx1,idx2),:)

and now it works just fine.

Regards,


"Matt Fig" <spamanon@yahoo.com> wrote in message <gm04td$d6d$1@fred.mathworks.com>...
> One method:
>
> a=[1 34
> 2 33
> 2 45
> 3 32
> 4 65
> 4 99
> 5 11
> 1 33
> 6 22];
>
> sa = sortrows(a,1);
> [idx1,idx1] = unique(sa(:,1),'first');
> [idx2,idx2] = unique(sa(:,1),'last');
>
> newa = sa(idx1==idx2,:)
>
>
>
>
>
> c)]\cf^\dgzhTcjj^5ozg`zpz"zjz\jb]oj\"o`hzd\tiqzpH;@ijk\\``k

Subject: finding duplicate entires and removing them

From: Matt Fig

Date: 31 Jan, 2009 01:26:02

Message: 4 of 6

Actually, this can be simplified to:

[idx1,idx1] = unique(a(:,1),'first')
[idx2,idx2] = unique(a(:,1),'last')
a(idx1(idx1==idx2),:)








<jai{heeUkIq{{]l^p#dk{k]gaidpc*{q^]aa]{dA]rl]p6ukk]__{j{h#k

Subject: finding duplicate entires and removing them

From: Matt Fig

Date: 31 Jan, 2009 01:31:03

Message: 5 of 6

"Phanikumar Bhamidipati" <phanibhamidipati.remove@gmail.com> wrote in message <gm08dq$ah2$1@fred.mathworks.com>...
> Hi Matt,
> Thanks for the suggestion. I found that with one correction, i got what i wanted.


Yes, sorry for the typo. The second solution I posted was cleaner anyway, I hope it works for you.




hwl`ggm]w]wE[gqYwQdYf=l&~w`hw[]lYZfaYY`YZngecmYa2gd~]8g_wwe

Subject: finding duplicate entires and removing them

From: Roger Stafford

Date: 31 Jan, 2009 01:54:01

Message: 6 of 6

"Phanikumar Bhamidipati" <phanibhamidipati.remove@gmail.com> wrote in message <glvvv9$350$1@fred.mathworks.com>...
> ......
> I have an array of containing the two columns of data, first one is the index and the second one is the value. I want to find and remove any entires that have been repeated.
> ......

  Phanikumar, I have assumed here that you meant to only retain rows in which each number differs from all others in the corresponding column. Looking at Matt's solution I see that his assumption differs in that a row is retained if just the first number is never repeated in that first column. Now that I think about it, probably Matt guessed the right interpretation, but I will give you my solution anyway just in case you meant it the other way. There is no way to tell for sure which you meant from your example, and you said only that you wanted to "remove any entires that have been repeated."

 [t,p] = sort(a);
 q1 = 1:n; q1(p(:,1)) = q1;
 q2 = 1:n; q2(p(:,2)) = q2;
 d = diff(t);
 r = [ones(1,2);d]&[d;ones(1,2)];
 b = a(r(q1,1)&r(q2,2),:);

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