我有,D是不匹配的字符串数组
的单元格数组A =
{'abc';
'acd' ;
'aaa'}
B =
{'baa' ;
'bbb'}
C =
{'acc';
'aaa';
'aad'}
D = { {A}, {B}, {C}}
A,B和C的大小不同,我首先不知道它们的大小,所以我将它们组合成D.如何将D转换为单个单元格数组,以便我可以将它们写入excel表?
即。
D =
{'abc' 'baa' 'acc';
'acd' 'bbb' 'aaa';
'aaa' ' ' 'aad'}
答案 0 :(得分:1)
您可以让MATLAB在需要时扩展单元阵列:
F={};
for k = 1:numel(D),
F(1:numel(D{k}), end+1)=D{k};
end
导致
F =
'abc' 'baa' 'acc'
'acd' 'bbb' 'aaa'
'aaa' [] 'aad'
如果将空矩阵作为空字符串很重要,请使用
>> F(cellfun('isempty', F))={''}
F =
'abc' 'baa' 'acc'
'acd' 'bbb' 'aaa'
'aaa' '' 'aad'
答案 1 :(得分:0)
这将是一种矢量化方法 -
D = { {A}, {B}, {C}} %// Code from question (not tinkering with it)
%// Extract all cells data at each cell index position.
%// In other words, reduce level of cell data storage by one step.
D1 = vertcat(D{:})
%// Get lengths of each cell
lens = cellfun('length',D1)
%// Initialize output cell array
out_cell = cell(max(lens),numel(lens))
%// Mask of valid cell positions in the output
mask = bsxfun(@le,[1:max(lens)]',lens(:).')
%// Store concatenated cell data into masked positions in output
out_cell(mask) = vertcat(D1{:})
示例运行 -
>> A,B,C
A =
'abc'
'acd'
'aaa'
B =
'baa'
'bbb'
C =
'acc'
'aaa'
'aad'
>> out_cell
out_cell =
'abc' 'baa' 'acc'
'acd' 'bbb' 'aaa'
'aaa' [] 'aad'
请注意,如果您在开始时这样做,则可以完全避免创建D
-
D1 = {A,B,C}