2011-11-14 5 views
0

매트릭스를 대략적인 양의 행으로 파티셔닝하고 싶습니다. 예를 들어,이 치수가 155 x 1000 인 행렬이있는 경우, 각 새 행렬의 치수가 15 X 1000 인 10을 어떻게 나눌 수 있습니까?Matlab 매트릭스 파티셔닝

+0

임의성 때문에 파티션에 0 또는 20 개 이상의 행이있을 수 있음)? – k107

+0

@ kristi의 덧글을 펼치면 파티션 크기가 가변적 이도록하여 추가 용량을 처리하기 위해 모두 비슷하거나 동일한 크기의 파티션과 다른 크기 중 하나를 더합니까? –

답변

0

이 방법에 대해 :

"대략 심지어"당신이 어떤 파티션이 15 개 행을해야하고 일부는 16 개 행이 나 때문에 (당신은 무작위로 파티션에 각 행을 지정하려면 어떻게해야한다는 뜻으로
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)을 사용할 수는 있지만 읽기가 약간 어렵습니다.