将单元格数组列转换为矩阵列 [英] Convert cell array columns into matrix columns

查看:88
本文介绍了将单元格数组列转换为矩阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元格数组(16x5),我想提取保存在该单元格数组每一列中的所有值,并将它们放入矩阵内的一列中,以便保留这些列(即每个列的新矩阵列单元格列).

I have a cell array (16x5) and I would like to extract all the values held in each column of the cell array and place them into a column within a matrix such that the columns are preserved (i.e. new matrix column for each cell array column).

做到这一点的最佳方法是什么?

What is the best way to do this?

我尝试过:

for k=1:Samples
data(k,:) = [dist{:,k}];
end

但这会返回错误

Subscripted assignment dimension mismatch.

但是我不确定为什么.

However I am not sure why.

编辑-单元格数组结构:

EDIT - Cell array structure:

推荐答案

由于您的循环代码有效,因此我认为发生了错误,因为data已预先分配了尺寸,且尺寸与逗号扩展的列(Matlab将使用显式索引而不使用:运算符来增长矩阵). 您只需要在以逗号分隔的扩展名之后获取数据的长度即可:

Since your loop code is valid, I assume the error is being raised because data is preallocated with dimensions not matching the length of the comma-expanded dist column (Matlab will grow matrices with explicit indices but not with the : operator). You just need to get the length of the data after the comma-separated expansion:

nElem   = numel([dist{:,1}]);
Samples = size(dist,2);
data    = zeros(Samples,nElem);  
for k=1:Samples
  data(k,:) = [dist{:,k}];
end

或者如果您希望在列中输入

Or if you want it in columns

data = zeros(nElem,Samples);  
for k=1:Samples
  data(:,k) = [dist{:,k}]';
end

这篇关于将单元格数组列转换为矩阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆