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:
Sorting Strings and uniquifying

Subject: Sorting Strings and uniquifying

From: Pat Finder

Date: 19 Mar, 2010 16:33:06

Message: 1 of 4

I have a bunch of image, for each image I want to associate a subject string.
Forming a cell array or an array of strings -- not a problem.

Then I want to sort the strings, and give each unique string a unique incremental identifier.

input = { 'cat', 'cat', 'dog', 'dog', 'bird', 'goose', 'cat', 'goose' };
output = { 1, 1, 2, 2, 3, 4, 1, 4 }.

I cannot find anything in the documentation that helps me manipulate or sort strings.
What is the best way to do my conversion?

Thanks for your attention.

Subject: Sorting Strings and uniquifying

From: Oleg Komarov

Date: 19 Mar, 2010 16:51:04

Message: 2 of 4

> Then I want to sort the strings, and give each unique string a unique incremental identifier.
>
> input = { 'cat', 'cat', 'dog', 'dog', 'bird', 'goose', 'cat', 'goose' };
> output = { 1, 1, 2, 2, 3, 4, 1, 4 }.

if you sort input, 'bird' comes first and not third as you pointed out in the output.

[a,out,out] = unique(input)
a =
    'bird'
    'cat'
    'dog'
    'goose'

out =
          2.00
          2.00
          3.00
          3.00
          1.00
          4.00
          2.00
          4.00

Oleg

Subject: Sorting Strings and uniquifying

From: us

Date: 19 Mar, 2010 17:00:05

Message: 3 of 4

"Pat Finder" <pfinder@netacc.net> wrote in message <ho0902$f65$1@fred.mathworks.com>...
> I have a bunch of image, for each image I want to associate a subject string.
> Forming a cell array or an array of strings -- not a problem.
>
> Then I want to sort the strings, and give each unique string a unique incremental identifier.
>
> input = { 'cat', 'cat', 'dog', 'dog', 'bird', 'goose', 'cat', 'goose' };
> output = { 1, 1, 2, 2, 3, 4, 1, 4 }.
>
> I cannot find anything in the documentation that helps me manipulate or sort strings.
> What is the best way to do my conversion?
>
> Thanks for your attention.

one of the solutions
- to get ...exactly... what you asked for...

     c={'cat','cat','dog','dog','bird','goose','cat','goose'};
     [cu,cg,cx]=unique(c,'first');
     cx=cg(cx);
     [ix,ix,ix]=unique(cx);
     disp(ix);
% 1 1 2 2 3 4 1 4

us

Subject: Sorting Strings and uniquifying

From: Pat Finder

Date: 19 Mar, 2010 18:23:02

Message: 4 of 4

"us " <us@neurol.unizh.ch> wrote in message <ho0ail$c2k$1@fred.mathworks.com>...
> "Pat Finder" <pfinder@netacc.net> wrote in message
> [snip]
>
> one of the solutions
> - to get ...exactly... what you asked for...
>
> c={'cat','cat','dog','dog','bird','goose','cat','goose'};
> [cu,cg,cx]=unique(c,'first');
> cx=cg(cx);
> [ix,ix,ix]=unique(cx);
> disp(ix);
> % 1 1 2 2 3 4 1 4
>
> us

Both of these solutions work for me.
Thank you both very much.

I didn't realize that sort would work on strings.

Best Regards,
- Pat

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