From: Pat Finder on
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.
From: Oleg Komarov on
> 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
From: us on
"Pat Finder" <pfinder(a)netacc.net> wrote in message <ho0902$f65$1(a)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
From: Pat Finder on
"us " <us(a)neurol.unizh.ch> wrote in message <ho0ail$c2k$1(a)fred.mathworks.com>...
> "Pat Finder" <pfinder(a)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