我有一个大的2D矩阵,比方说,1000乘10。我试图把这个矩阵分解成多个2D矩阵。它们可以是10乘10,5乘10或23乘10。列的大小没有变化,我根据第一列中的数字来分割它们。(在我的实际情况下,我只对前两列这样做。)换句话说,我使用相同的id标记放置行,这是向量的第一个列值。
我正试图通过for循环来实现这一点。我认为最好将它们放入“单元数组”,因为单元数组允许用户拥有不同维度和不同类型的数据。所以,我想要一个单元(1倍变化的-N?)其中N是分裂矩阵的个数,有N个不同大小的2D矩阵。
我的问题是
具有不同单元格维的引用并不多。如有任何建议/答案,将不胜感激。
发布于 2014-05-14 22:21:47
下面是一个简单的示例,向您展示如何将数组输出到单元格中:
A=cell(2,1); % create a 2x1 cell array
A{1}=ones(3); % put a 3x3 matrix into the first cell
A{2}=rand(5); % put a 5x5 array into the second cell
A{3}=zeros(2); % grow the cell array to 3x1 and put a 2x2 matrix in the third cell
A % see what A looks like因此,您可以在for循环中生长单元数组,例如:
A=cell(5,1);
for i=1:5
A{i}= ...
end发布于 2014-05-14 22:36:53
myCell = cell(44,1)初始化单元格数组。myCell{4} = data(1:23, :)填充单元格。例如,您可以编写以下代码:
% Create some arbitrary data:
data = (1:1000)'*(1:10);
blockSize = 23;
% Create a cell array from scratch:
myCell2 = cell(ceil(1000/blockSize),1);
for ind = 1:ceil(1000/blockSize)
myCell2{ind} = data((ind-1)*blockSize +1 : min(ind*blockSize, end),:);
end但是,我建议避免使用for循环,而是使用函数mat2cell(...),如下所示:
% Create some arbitrary data:
data = (1:1000)'*(1:10);
blockSize = 23;
% Break it up with mat2cell:
myCell = mat2cell(data, [ones(44,1)*23; 11], 10)
% And just for fun, do a calcuation:
cellfun(@norm, myCell)https://stackoverflow.com/questions/23665908
复制相似问题