0
votes

I want to split any matrix into equal parts of sub matrices. I used mat2cell to do that. But it makes most of the sub matrices as desired size but the some parts are not equal. For example: I have a matrix of size 973*973. I want to divide equally into 216*216 sub matrices. When I apply mat2cell, It makes a 6*6 sub matrices. In which first 5*5 are 216*216 sub matrices. But the blocks in last row and columns are not 216*216. They are random sizes. Which is not desired.I want every sub matrices will have equal number of rows and column. Every blocks will be 216*216 size or what specified by the user.

I=imread(file);
[rows columns numberOfColorBands] = size(I);
blockSizeR = 256; % Rows in desired block which can be anynumber.
blockSizeC = 256; % Columns in desired block which can be anynumber.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];

ca = mat2cell(I, blockVectorR, blockVectorC);
1
you know that 973/216 is not an integer right ? So how could you divide equally such a matrix ? There is no miracle here.obchardon
@obchardon I know. So i have to resize the matrix so that i can equally split, this is the only way ?user6934433
It depend on your needs, you can interpolate your matrix to fit a n*216 size, you can divides your matrix into 49 139*139 submatrices,...obchardon
@obchardon Yes I think its better to take n*216 size which is closest to my matrix dimension. Like 4*216 = 864 So i can resize the matrix in to 864*864user6934433
So you can use imresizeobchardon

1 Answers

0
votes

If you're just trying to pad the empty spaces with zeros, the easiest way is just to pad the initial matrix before splitting it:

I=imread(file);
[rows columns numberOfColorBands] = size(I);
blockSizeR = 256; % Rows in desired block which can be anynumber.
blockSizeC = 256; % Columns in desired block which can be anynumber.

% Figure out the size of each block in rows.
blockRows = ceil(rows / blockSizeR);
blockVectorR = blockSizeR * ones(1, blockRows);

% Figure out the size of each block in columns.
blockCols = ceil(columns / blockSizeC);
blockVectorC = blockSizeC * ones(1, blockCols);

% Pad with zeros
I(blockRows*blockSizeR,blockCols*blockSizeC) = 0;

% Split it up
ca = mat2cell(I, blockVectorR, blockVectorC);

I think that padding with zeros is the best way forward, because otherwise you may be faced with an I with two prime dimensions that cannot be split

If you'd rather pad with NaN, just substitute this into the above code where I pad with zeros:

% Pad with NaN
I = [I, NaN(size(I,1), blockCols*blockSizeC-size(I,2)) ;...
NaN(blockRows*blockSizeR-size(I,1), size(I,2)) NaN(blockRows*blockSizeR-size(I,1),blockCols*blockSizeC-size(I,2))];