2011-11-14 189 views
0

我想通过近似均匀的行数来划分矩阵。例如,如果我有一个由这些维度155 x 1000组成的矩阵,那么我怎样才能将它划分为10,其中每个新矩阵的近似维数为15 X 1000?Matlab矩阵分区

+0

通过“近似,甚至”你的意思是一些分区应该有15行和一些应该有16行或做你想做的每一行随机分配到一个分区(使由于随机性,分区可能有0或20或更多的行)? – k107

+0

扩展@ kristi的评论,你是否想要分区大小的变化,以便他们都是相似的,或相同大小的分区加上一个不同的大小,以处理额外? –

回答

0

如何:

inMatrix = rand(155, 1000); 
numRows = size(inMatrix, 1); 
numParts = 10; 

a = floor(numRows/numParts);   % = 15 
b = rem(numRows, numParts);   % = 5 
partition = ones(1, numParts)*a;  % = [15 15 15 15 15 15 15 15 15 15] 
partition(1:b) = partition(1:b)+1; % = [16 16 16 16 16 15 15 15 15 15] 
disp(sum(partition))     % = 155 

% Split matrix rows into partition, storing result in a cell array 
outMatrices = mat2cell(inMatrix, partition, 1000) 

outMatrices = 
[16x1000 double] 
[16x1000 double] 
[16x1000 double] 
[16x1000 double] 
[16x1000 double] 
[15x1000 double] 
[15x1000 double] 
[15x1000 double] 
[15x1000 double] 
[15x1000 double] 
0

这是你想要的吗?

%Setup 
x = rand(155,4); %4 columns prints on my screen, the second dimension can be any size 
n = size(x,1); 
step = round(n/15); 

%Now loop through the array, creating partitions 
% This loop just displays the partition plus a divider 
for ixStart = 1:step:n 
    part = x( ixStart:(min(ixStart+step,end)) , : ); 
    disp(part); 
    disp('---------') 
end 

这里唯一的问题是在一个下标功能评价中使用end关键字。如果没有使用关键字,你可以使用size(x,1),但这有点难以阅读。