0

I would like to partition a matrix by an approximate even amount of rows. For example, if I have a matrix by these dimensions 155 x 1000, how can I partition it by 10 where each new matrix has the approximate dimensions 15 X 1000?

lurodrig
  • 99
  • 3
  • 8
  • By "approximate even" do you mean that some partitions should have 15 rows and some should have 16 rows or do you want to randomly assign each row to a partition (so that because of the randomness, a partitions could have 0 or 20 or more rows)? – k107 Nov 14 '11 at 22:33
  • Expanding on @kristi's comment, do you want variability in the partition size so they are all similar, or equal size partitions plus one of a different size, to deal with the extra? – reve_etrange Nov 15 '11 at 05:23

2 Answers2

0

Is this what you want?

%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

The only trick here is the use of the end keyword within a function evaluation in the subscripting. Without using the keyword you could use size(x,1), but that is a bit harder to read.

Pursuit
  • 12,285
  • 1
  • 25
  • 41
0

How about this:

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]
Sam Roberts
  • 23,951
  • 1
  • 40
  • 64