想我不妨写一个答案...您可以使用 strrep 将 'a' 替换为 '1' (注意它是字符串格式),并对所有 26 个字母执行此操作,然后使用 cell2mat 转换字符串 '1' - '26' 等到数字 1 -26。
让我们说:
t = {'a','b','c'} //%Array of Strings
t = strrep(t,'a','1') //%replace all 'a' with '1'
t = strrep(t,'b','2') //%replace all 'b' with '2'
t = strrep(t,'c','3') //%replace all 'c' with '3'
%// Or 1 line:
t = strrep(g,{'a','b','c'},{'1','2','3'})
>> t =
'1' '2' '3'
output = cellfun(@str2num,t,'un',0) //% keeps the cell structure
>> output =
[1] [2] [3]
或者:
output = str2num(cell2mat(t')) //% uses the matrix structure instead, NOTE the inversion ', it is crucial here.
>> output =
1
2
3