结合Cell Array Matlab的列

时间:2014-03-15 00:52:02

标签: matlab

我有一个具有动态行大小和列大小的2D单元阵列。一个例子:

cell3 = [{'abe' 'basdasd' 'ceee'}; {'d' 'eass' 'feeeeeeeeee'}]

给出:(尺寸2乘3)

'abe'    'basdasd'    'ceee'       
'd'      'eass'       'feeeeeeeeee'

我希望能够组合列并重现聚合字符串单元格数组,其中每个字符串由单个空格分隔。知道怎么做吗?

所以我想要的输出是:

'abe basdasd ceee'       
'd eass feeeeeeeeee'

最终尺寸是2乘1。

这可能吗?

2 个答案:

答案 0 :(得分:4)

在循环或strjoin中应用cellfun。后者:

>> cellRows = mat2cell(cell3,ones(size(cell3,1),1),size(cell3,2));
>> out = cellfun(@strjoin,cellRows,'uni',0)
out = 
    'abe basdasd ceee'
    'd eass feeeeeeeeee'

答案 1 :(得分:1)

没有循环的解决方案或cellfun

[m n] = size(cell3);
cellCols = mat2cell(cell3,m,ones(1,n)); %// group by columns
cellColsSpace(1:2:2*size(cell3,2)-1) = cellCols; %// make room (columns)...
cellColsSpace(2:2:2*size(cell3,2)-1) = {{' '}}; %// ...to introduce spaces
%// Note that a cell within cell is needed, because of how strcat works.
result = strcat(cellColsSpace{:});