Arrays Matlab:将单元的单元阵列转换为单个单元阵列
我知道,D是一个由不匹配的字符串单元格数组组成的单元格数组Arrays Matlab:将单元的单元阵列转换为单个单元阵列,arrays,matlab,cell-array,Arrays,Matlab,Cell Array,我知道,D是一个由不匹配的字符串单元格数组组成的单元格数组 A = {'abc'; 'acd' ; 'aaa'} B = {'baa' ; 'bbb'} C = {'acc'; 'aaa'; 'aad'} D = { {A}, {B}, {C}} A、 B和C的大小不同,我一开始不知道它们的大小,所以我将它们组合成D。如何将D转换成单个单元格数组,以便将它们写入excel表格 i、 e 需要时,可以让MATLAB展开单元阵列: F={}; for k = 1
A =
{'abc';
'acd' ;
'aaa'}
B =
{'baa' ;
'bbb'}
C =
{'acc';
'aaa';
'aad'}
D = { {A}, {B}, {C}}
A、 B和C的大小不同,我一开始不知道它们的大小,所以我将它们组合成D。如何将D转换成单个单元格数组,以便将它们写入excel表格
i、 e
需要时,可以让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'
这将是一种矢量化方法-
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}
因此,在所需的输出中,假设我们开始在Excel中将D写入A1,A1将是“abc”,B3将是“”?感谢您的回复。B3将是空的。
D1 = {A,B,C}