2011-11-14 29 views
0

行列をおおよそ偶数行で分割したいと思います。たとえば、これらの次元が155×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)を使用することはできますが、それは少し読みにくいです。

このことについてどのように