单元格数组(不同大小的单元格)到矩阵
Cell array (different size cells) to matrix
我正在尝试将单元格内容大小不同的单元格数组转换为矩阵。我尝试了以下代码(来自 previous question):
tcell = {[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6], [1], []}; %# Sample array
maxSize = max(cellfun(@numel,tcell)); %# Get the maximum vector size
fcn = @(x) [x nan(1,maxSize-numel(x))]; %# Create an anonymous function
rmat = cellfun(fcn,tcell,'UniformOutput',false); %# Pad each cell with NaNs
rmat = vertcat(rmat{:}) %# Vertically concatenate cells
我收到以下错误代码:
Dimensions of matrices being concatenated are not consistent.
Error in @(x)[x,nan(1,maxSize-numel(x))]
我认为我的元胞数组与测试示例的内容不同(请参阅说明):在 MATLAB 中查看时我的元胞数组(1x31 元胞)的内容是
30x1 cell 40x1 cell 37x1 cell
我是否必须先对元胞数组进行另一次转换?如何将元胞数组转换为 tcell 的形式?
我已经搜索了一段时间,但我还不熟悉所有术语。解决方案可能很简单,但我还没有看到它的知识。欢迎所有输入!
感谢评论员的输入,我找到了以下答案:
A = cellfun(@transpose,Arad,'UniformOutput',false); %transpose the cell array
maxSize = max(cellfun(@numel,A)); %# Get the maximum vector size
fcn = @(x) [x nan(1,maxSize-numel(x))]; %# Create an anonymous function rmat = cellfun(fcn,A,'UniformOutput',false); %# Pad each cell with NaNs
rmat = horzcat(rmat{:}) ; % concatenate horizontally
rmat = horzcat(rmat{:}) ; % concatenate horizontally again
rmat = reshape(rmat,maxSize, []);% reshape to m(=maxsize)x n(determined by total number of elements/number of rows(maxsize)
实际上你的原始代码几乎是完美的,但是对于线向量。对于单元格中的列向量,您漏掉了一个分号。
maxSize = max(cellfun(@numel,tcell)); %# Get the maximum vector size
fcn = @(x) [x ; nan(1,maxSize-numel(x))]; %# semicolon here
rmat = cellfun(fcn,tcell,'UniformOutput',false); %# Pad each cell with NaNs
我正在尝试将单元格内容大小不同的单元格数组转换为矩阵。我尝试了以下代码(来自 previous question):
tcell = {[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6], [1], []}; %# Sample array
maxSize = max(cellfun(@numel,tcell)); %# Get the maximum vector size
fcn = @(x) [x nan(1,maxSize-numel(x))]; %# Create an anonymous function
rmat = cellfun(fcn,tcell,'UniformOutput',false); %# Pad each cell with NaNs
rmat = vertcat(rmat{:}) %# Vertically concatenate cells
我收到以下错误代码:
Dimensions of matrices being concatenated are not consistent.
Error in
@(x)[x,nan(1,maxSize-numel(x))]
我认为我的元胞数组与测试示例的内容不同(请参阅说明):在 MATLAB 中查看时我的元胞数组(1x31 元胞)的内容是
30x1 cell 40x1 cell 37x1 cell
我是否必须先对元胞数组进行另一次转换?如何将元胞数组转换为 tcell 的形式?
我已经搜索了一段时间,但我还不熟悉所有术语。解决方案可能很简单,但我还没有看到它的知识。欢迎所有输入!
感谢评论员的输入,我找到了以下答案:
A = cellfun(@transpose,Arad,'UniformOutput',false); %transpose the cell array
maxSize = max(cellfun(@numel,A)); %# Get the maximum vector size
fcn = @(x) [x nan(1,maxSize-numel(x))]; %# Create an anonymous function rmat = cellfun(fcn,A,'UniformOutput',false); %# Pad each cell with NaNs
rmat = horzcat(rmat{:}) ; % concatenate horizontally
rmat = horzcat(rmat{:}) ; % concatenate horizontally again
rmat = reshape(rmat,maxSize, []);% reshape to m(=maxsize)x n(determined by total number of elements/number of rows(maxsize)
实际上你的原始代码几乎是完美的,但是对于线向量。对于单元格中的列向量,您漏掉了一个分号。
maxSize = max(cellfun(@numel,tcell)); %# Get the maximum vector size
fcn = @(x) [x ; nan(1,maxSize-numel(x))]; %# semicolon here
rmat = cellfun(fcn,tcell,'UniformOutput',false); %# Pad each cell with NaNs