[英]Integrating matrix vectors into a cell array using MATLAB?
I have this cell array of matrices: 我有矩阵的这个单元格数组:
a = [14x16 double] [14x17 double] [14x27 double][14x62 double] [14x16 double]
Unfortunately, I don't want that. 不幸的是,我不想那样。 What I need is to get all the vectors in the matrix and get a large cell array of vectors.
我需要的是获取矩阵中的所有向量,并获取向量的大单元格数组。
How can I achieve this? 我该如何实现?
I need an array of size 16+17+27+62+16 where every vector has 14 elements. 我需要一个大小为16 + 17 + 27 + 62 + 16的数组,其中每个向量都有14个元素。
I haven't used matlab a lot before, I am sure this is sort of trivial. 我以前没用过matlab,我敢肯定这是微不足道的。 Can someone help?
有人可以帮忙吗?
You can call num2cell
on each of the cell arrays, and then concatenate them all using cat
. 您可以在每个单元格数组上调用
num2cell
,然后使用cat
将它们全部串联起来。
%// Generate some test data
a = arrayfun(@(x)rand(14, x), [16 17 27 62 16] , 'uni', 0);
[14x16 double] [14x17 double] [14x27 double] [14x62 double] [14x16 double]
%// Now merge them into a cell array of vectors
newcell = cellfun(@(x)num2cell(x, 1), a, 'uni', 0);
newcell = cat(2, newcell{:});
And just to verify that everything is the dimension we expect 只是为了验证所有内容都是我们期望的尺寸
isequal(size(newcell), [1 16+17+27+62+16])
1
isequal(size(newcell{1}), [14 1])
1
If instead you simply want a large matrix, you can just concatenate the initial data into a matrix (along the second dimension): 相反,如果您只想要一个大矩阵,则可以将初始数据连接到一个矩阵中(沿着第二维):
matrix = cat(2, a{:});
size(matrix)
14 138
I would recommend the matrix approach as opposed to the cell array approach as MATLAB is highly optimized to perform operations on matrices. 我建议使用矩阵方法,而不是单元阵列方法,因为MATLAB已高度优化以对矩阵执行运算。 You will definitely take a performance hit using cell arrays instead.
绝对会使用单元阵列来提高性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.