如何根据订单编号字符串

Mac*_*nos 4 sorting string matlab cell-array

我在matlab中有一个字符串的单元格数组.有些字符串可能相同.我想用字典方式对字符串进行编号.

例如,如果我有{'abc','aty','utf8','sport','utf8','abc'},在输出中我想得到数组[1, 2, 4, 3, 4, 1].

你能给我任何方法吗?

Not*_*hat 8

重复的字符串使用sort棘手,但在这种情况下,您可以依赖于unique适用于字符串的单元格数组的事实,并且两者都对其输出进行排序,并可选地返回原始输入中这些已排序元素的索引:

>> 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
Run Code Online (Sandbox Code Playgroud)

或者你显然可以使用,[~, ~, index] = unique(a);如果你真的想要索引.