How to specify strings according to their order

I have an array of row cells in matlab. Some lines may be equal. I want the number of lines to be lexicographical.

For example, if I have {'abc','aty','utf8','sport','utf8','abc'}, at the output I want to get an array [1, 2, 4, 3, 4, 1].

Can you give me some approach?

+4
source share
1 answer

Duplicated rows make use sortdifficult, but in this case you can rely on what uniqueworks for arrays of row cells and both types of output and optionally returns the indices of these 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

[~, ~, index] = unique(a);, .

+8

Source: https://habr.com/ru/post/1530857/


All Articles