水平串联字符串的单元格数组
问题描述:
我希望水平连接字符串单元格数组的行,如下所示.
I wish to horizontally concatenate lines of a cell array of strings as shown below.
start = {'hello','world','test';'join','me','please'}
finish = {'helloworldtest';'joinmeplease'}
是否有内置函数可以完成上述转换?
Are there any built-in functions that accomplish the above transformation?
答
一种简单的方法也是在行上循环
A simple way is too loop over the rows
nRows = size(start,1);
finish = cell(nRows,1);
for r = 1:nRows
finish{r} = [start{r,:}];
end
编辑
一种更复杂,更难读的解决方案(一般解决方案留给读者练习)
A more involved and slightly harder to read solution that does the same (the general solution is left as an exercise for the reader)
finish = accumarray([1 1 1 2 2 2]',[ 1 3 5 2 4 6]',[],@(x){[start{x}]}
)