I have a cell array of strings in matlab. Some strings may be equal. I want to number strings in the lexicographical way.
For example, if I have {'abc','aty','utf8','sport','utf8','abc'}
, in the output I want to get the array [1, 2, 4, 3, 4, 1]
.
Can you give me any approach?
Duplicated strings make using sort
tricky, but in this case you can rely on the fact that unique
works for cell arrays of strings, and both sorts its output and optionally returns the indices of those sorted elements in the original input:
>> a = {'abc' 'aty' 'utf8' 'sport' 'utf8' 'abc'}
a =
{
[1,1] = abc
[1,2] = aty
[1,3] = utf8
[1,4] = sport
[1,5] = utf8
[1,6] = abc
}
>> [b, ~, index] = unique(a)
b =
{
[1,1] = abc
[1,2] = aty
[1,3] = sport
[1,4] = utf8
}
index =
1 2 4 3 4 1
or you can obviously just use [~, ~, index] = unique(a);
if you really only want the indices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With