什么是matlab的内置函数的替代品:strrep

时间:2016-06-08 23:10:02

标签: matlab

看着手机键盘。每个数字都有三个字母与之关联,除了1和0.如何在不使用内置函数'strrep'的情况下用相关数字替换字母?如果输入是MATLAB,输出将是628522 感谢

1 个答案:

答案 0 :(得分:2)

使用containers.Map非常容易:

c = containers.Map({'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}, ...
    [2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9]) ;

testStr = 'MATLAB' ;
testOut = NaN(1,numel(testStr)) ;
for ii = 1:numel(testStr)
    testOut(ii) = c(lower(testStr(ii))) ;
end

结果:

>> testOut

testOut =

     6     2     8     5     2     2