2011-11-14 238 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),但這有點難以閱讀。