Mix letters of a string in alphabetical order in matlab

六月ゝ 毕业季﹏ 提交于 2019-12-25 02:18:09

问题


I have the cell array of strings in matlab. I want to sort letters in every string in alphabetical order. How can I do that?

For example, if I have ['dcb','aetk','acb'}], I want it to be: ['bcd','aekt','abc'].


回答1:


The handy helper here is cellfun, with the correct option for nonscalar output - we tell it to run sort on each element of the cell array in turn:

>> a = {'dcb' 'aetk' 'acb'}
a =
{
  [1,1] = dcb
  [1,2] = aetk
  [1,3] = acb
}

>> b = cellfun(@sort, a, 'UniformOutput', false);
b =
{
  [1,1] = bcd
  [1,2] = aekt
  [1,3] = abc
}


来源:https://stackoverflow.com/questions/22284076/mix-letters-of-a-string-in-alphabetical-order-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!