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:
How to use "Unique"

Subject: How to use "Unique"

From: Kevin Ellis

Date: 12 Nov, 2012 17:10:24

Message: 1 of 4

Hello,

I am having trouble figuring out how to use "unique" to find the index of identical elements in a single column cell array. As an example, I have a cell array with the following data:

A =
    ''231234242800100043062.68'
    ''231234242800200043208.86'
    ''231234242800400042964.91'
    ''231234242800500038861.19'
    ''231234242800600010255.99'
    ''231234242800900054183.5'
    ''231234242801100077938.9'
    ''231234242801200049682.26'
    ''23123424800900054183.5'
    ''23123424800900054183.5'
    ''23123424GHA0200020256.36'

I have been trying to use unique to find the index of identical elements. In this case, the third and second to last values are equal to one another. I need to figure out a way to output the indices 9 & 10. I have tried using [C1, ia1, ib1] = unique(A,'first'), but still do not understand how "ib1" can be used to find the indices 9 & 10.

Also, the order is crucial. The order shown in the example cell array cannot change. I realize this may be a question for the help file, but after looking at it I still do not fully understand so I am hoping someone could help. Thanks.

Kevin

Subject: How to use "Unique"

From: dpb

Date: 12 Nov, 2012 17:40:37

Message: 2 of 4

On 11/12/2012 11:10 AM, Kevin Ellis wrote:
...
> I am having trouble figuring out how to use "unique" to find the index
> of identical elements in a single column cell array. ...
...
> Also, the order is crucial. The order shown in the example cell array
> cannot change. I realize this may be a question for the help file, but
> after looking at it I still do not fully understand so I am hoping
> someone could help. Thanks.
...

unique() will give you the values that are, well, unique to the set.
Once you have them you can then use findstr() and/or ismember() to
locate the locations of those values in the original vector.

Or, histc for numeric values for the bins obtained from unique() but
your data are/must be character in the above example...

--

Subject: How to use "Unique"

From: Doug Schwarz

Date: 12 Nov, 2012 17:42:01

Message: 3 of 4

In article <k7rai0$n1i$1@newscl01ah.mathworks.com>,
 "Kevin Ellis" <kevin.ellis86@gmail.com> wrote:

> Hello,
>
> I am having trouble figuring out how to use "unique" to find the index of
> identical elements in a single column cell array. As an example, I have a
> cell array with the following data:
>
> A =
> ''231234242800100043062.68'
> ''231234242800200043208.86'
> ''231234242800400042964.91'
> ''231234242800500038861.19'
> ''231234242800600010255.99'
> ''231234242800900054183.5'
> ''231234242801100077938.9'
> ''231234242801200049682.26'
> ''23123424800900054183.5'
> ''23123424800900054183.5'
> ''23123424GHA0200020256.36'
>
> I have been trying to use unique to find the index of identical elements. In
> this case, the third and second to last values are equal to one another. I
> need to figure out a way to output the indices 9 & 10. I have tried using
> [C1, ia1, ib1] = unique(A,'first'), but still do not understand how "ib1" can
> be used to find the indices 9 & 10.
>
> Also, the order is crucial. The order shown in the example cell array cannot
> change. I realize this may be a question for the help file, but after looking
> at it I still do not fully understand so I am hoping someone could help.
> Thanks.
>
> Kevin

Try this:

[~,~,ib1] = unique(A);
j = find(histc(ib1,1:max(ib1)) > 1);
dupes = find(ismember(ib1,j));

It will find ALL indices of items in A that are duplicated, even if
there is more than one such unique item, i.e., if

A = {'a','a','b','c','c'}

then dupes will be [1 2 4 5];

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: How to use "Unique"

From: Kevin Ellis

Date: 12 Nov, 2012 18:36:18

Message: 4 of 4

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-8B363E.12420112112012@news.eternal-september.org>...
> In article <k7rai0$n1i$1@newscl01ah.mathworks.com>,
> "Kevin Ellis" <kevin.ellis86@gmail.com> wrote:
>
> > Hello,
> >
> > I am having trouble figuring out how to use "unique" to find the index of
> > identical elements in a single column cell array. As an example, I have a
> > cell array with the following data:
> >
> > A =
> > ''231234242800100043062.68'
> > ''231234242800200043208.86'
> > ''231234242800400042964.91'
> > ''231234242800500038861.19'
> > ''231234242800600010255.99'
> > ''231234242800900054183.5'
> > ''231234242801100077938.9'
> > ''231234242801200049682.26'
> > ''23123424800900054183.5'
> > ''23123424800900054183.5'
> > ''23123424GHA0200020256.36'
> >
> > I have been trying to use unique to find the index of identical elements. In
> > this case, the third and second to last values are equal to one another. I
> > need to figure out a way to output the indices 9 & 10. I have tried using
> > [C1, ia1, ib1] = unique(A,'first'), but still do not understand how "ib1" can
> > be used to find the indices 9 & 10.
> >
> > Also, the order is crucial. The order shown in the example cell array cannot
> > change. I realize this may be a question for the help file, but after looking
> > at it I still do not fully understand so I am hoping someone could help.
> > Thanks.
> >
> > Kevin
>
> Try this:
>
> [~,~,ib1] = unique(A);
> j = find(histc(ib1,1:max(ib1)) > 1);
> dupes = find(ismember(ib1,j));
>
> It will find ALL indices of items in A that are duplicated, even if
> there is more than one such unique item, i.e., if
>
> A = {'a','a','b','c','c'}
>
> then dupes will be [1 2 4 5];
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Thanks for the quick response. That worked perfectly.

Kevin

Tags for this Thread

No tags are associated with 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