2016-02-12 75 views
1

我有一個單元陣列,其元素爲​​,"def","ghi"操作單元陣列元素

誰能告訴我怎麼把它轉換成"aa bb cc","dd ee ff","gg hh ii"?我在​​等結束。

+2

你有什麼企圖? –

+2

另外,爲了澄清,是你的細胞元素'''「abc」''或''abc''?換句話說,它們是否包含本身不用於創建Matlab字符串(char數組)的直的雙引號符號''「'? – horchler

回答

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 ' 

每個字符串後面都會有一個尾隨空白,但是您可以使用strtrim將其刪除。

>> strtrim(tnew) 

ans = 

'aa bb cc' 'dd ee ff' 
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' 
+0

我意識到我們不是在玩高爾夫代碼(不幸的是?:)),但它更短使用'deblank'而不是最後一行'deblank'也快幾個數量級 –

+1

@transversalitycondition好主意!無論如何,另一個問題就是這樣,所以我會離開它。 –