我有一个包含元素"abc"
,"def"
,"ghi"
的单元格数组。
有谁能告诉我如何将其转换为"aa bb cc"
,"dd ee ff"
,"gg hh ii"
?
我结束了"abcabc"
等等。
答案 0 :(得分:4)
如何使用常规表达式?
x = {'abc', 'def', 'ghi'}; %// cell array of strings
y = regexprep(x, '.', '$0$0 '); %// duplicate each character and insert a blank space
y = regexprep(y, ' $', ''); %// remove last space
这给出了
y =
'aa bb cc' 'dd ee ff' 'gg hh ii'
答案 1 :(得分:1)
>> t = {'abc','def'}
>> tnew = cellfun(@(x)reshape([x' x' repmat(' ',numel(x),1)]',1,[]),t,'UniformOutput',false)
tnew =
'aa bb cc ' 'dd ee ff '
There will be a trailing whitespace after each string but you can get rid of that using strtrim.
>> strtrim(tnew)
ans =
'aa bb cc' 'dd ee ff'