http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327
MATLAB Central Newsreader  Dividing a Matrix into submatrices
Feed for thread: Dividing a Matrix into submatrices
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 27 Jan 2012 04:20:10 +0000
Dividing a Matrix into submatrices
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327#864974
Kevin Ellis
Hello,<br>
<br>
I am having a problem using the "mat2cell" command. Suppose I have a 10x10 square matrix and I would like to divide that matrix into a set of 2x2 matrices. So there would be fifteen total submatrices. How would I set up the command for mat2cell to accomplish this? <br>
<br>
Also for understanding how you perform the same operation on a 6x6 matrix (that way I can see how the mat2cell command will change depending on the size of the square matrix)? <br>
<br>
Any help would be much appreciated. Thanks.<br>
<br>
Kevin

Fri, 27 Jan 2012 06:09:47 +0000
Re: Dividing a Matrix into submatrices
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327#864980
ImageAnalyst
On Jan 26, 8:20 pm, "Kevin Ellis" <kevin.elli...@gmail.com> wrote:<br>
> Hello,<br>
><br>
> I am having a problem using the "mat2cell" command. Suppose I have a 10x10 square matrix and I would like to divide that matrix into a set of 2x2 matrices. So there would be fifteen total submatrices. How would I set up the command for mat2cell to accomplish this?<br>
><br>
> Also for understanding how you perform the same operation on a 6x6 matrix (that way I can see how the mat2cell command will change depending on the size of the square matrix)?<br>
><br>
> Any help would be much appreciated. Thanks.<br>
><br>
> Kevin<br>
<br>
<br>
m = rand(10)<br>
<br>
mCell = mat2cell(m, [2 2 2 2 2], [2 2 2 2 2])<br>
<br>
I trust you can figure out how to make up the [2 2 2 2 2] or [2 2 2]<br>
vectors (in case you're using 6).

Fri, 27 Jan 2012 06:18:16 +0000
Re: Dividing a Matrix into submatrices
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327#864981
Kevin Ellis
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c8e60e6c8df640cf94743858ae9fba2c@c13g2000yqk.googlegroups.com>...<br>
> On Jan 26, 8:20 pm, "Kevin Ellis" <kevin.elli...@gmail.com> wrote:<br>
> > Hello,<br>
> ><br>
> > I am having a problem using the "mat2cell" command. Suppose I have a 10x10 square matrix and I would like to divide that matrix into a set of 2x2 matrices. So there would be fifteen total submatrices. How would I set up the command for mat2cell to accomplish this?<br>
> ><br>
> > Also for understanding how you perform the same operation on a 6x6 matrix (that way I can see how the mat2cell command will change depending on the size of the square matrix)?<br>
> ><br>
> > Any help would be much appreciated. Thanks.<br>
> ><br>
> > Kevin<br>
> <br>
> <br>
> m = rand(10)<br>
> <br>
> mCell = mat2cell(m, [2 2 2 2 2], [2 2 2 2 2])<br>
> <br>
> I trust you can figure out how to make up the [2 2 2 2 2] or [2 2 2]<br>
> vectors (in case you're using 6).<br>
<br>
Okay. Great. Thanks for the help.

Fri, 27 Jan 2012 14:27:53 +0000
Re: Dividing a Matrix into submatrices
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327#865006
Steven_Lord
<br>
<br>
"Kevin Ellis" <kevin.ellis86@gmail.com> wrote in message <br>
news:jft8lq$3n6$1@newscl01ah.mathworks.com...<br>
> Hello,<br>
><br>
> I am having a problem using the "mat2cell" command. Suppose I have a 10x10 <br>
> square matrix and I would like to divide that matrix into a set of 2x2 <br>
> matrices. So there would be fifteen total submatrices.<br>
<br>
25, actually.<br>
<br>
> How would I set up the command for mat2cell to accomplish this?<br>
<br>
ImageAnalyst already answered this, but if you're looking to evaluate a <br>
function on each of those blocks and you have Image Processing Toolbox, look <br>
at the BLOCKPROC (or for older versions, BLKPROC) function.<br>
<br>
<a href="http://www.mathworks.com/help/toolbox/images/ref/blockproc.html">http://www.mathworks.com/help/toolbox/images/ref/blockproc.html</a><br>
<br>
It will handle splitting your matrix into submatrices; you won't need to <br>
worry about MAT2CELL.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Fri, 27 Jan 2012 18:16:09 +0000
Re: Dividing a Matrix into submatrices
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327#865029
Matt J
"Kevin Ellis" wrote in message <jft8lq$3n6$1@newscl01ah.mathworks.com>...<br>
> Hello,<br>
> <br>
> I am having a problem using the "mat2cell" command. Suppose I have a 10x10 square matrix and I would like to divide that matrix into a set of 2x2 matrices. So there would be fifteen total submatrices. How would I set up the command for mat2cell to accomplish this? <br>
> <br>
> Also for understanding how you perform the same operation on a 6x6 matrix (that way I can see how the mat2cell command will change depending on the size of the square matrix)? <br>
====================<br>
<br>
When the submatrices are all the same size, as in your case, I find it more convenient to use the MAT2TILES function below, e.g.,<br>
<br>
>> mat2tiles(rand(10),[2,2])<br>
<br>
ans = <br>
<br>
[2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]<br>
[2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]<br>
[2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]<br>
[2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]<br>
[2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]<br>
<br>
>> mat2tiles(rand(6),[2,2])<br>
<br>
ans = <br>
<br>
[2x2 double] [2x2 double] [2x2 double]<br>
[2x2 double] [2x2 double] [2x2 double]<br>
[2x2 double] [2x2 double] [2x2 double]<br>
<br>
<br>
<br>
function outCell=mat2tiles(inArray,varargin)<br>
%MAT2TILES  breaks up an array into a cell array of adjacent subarrays of<br>
% equal sizes<br>
%<br>
% C=mat2tiles(X,D1,D2,D3,...,Dn)<br>
% C=mat2tiles(X,[D1,D2,D3,...,Dn])<br>
%<br>
%will produce a cell array C containing adjacent chunks of the array X,<br>
%with each chunk of dimensions D1xD2xD3x...xDn. If a dimensions Di does<br>
%not divide evenly into size(X,i), then the chunks at the upper boundary of<br>
%X along dimension i will be truncated.<br>
%<br>
%It is permissible for the Di to be given value Inf. When this is done, it is<br>
%equivalent to setting Di=size(X,i).<br>
%<br>
%If n < ndims(X), then the unspecified dimensions Dj, n<j<=ndims(X) will be<br>
%set to size(X,i).<br>
%<br>
%If n > ndims(X), the the extra dimensions Dj, j>ndims(X) will be ignored.<br>
<br>
tileSizes=[varargin{:}];<br>
<br>
N=length(tileSizes);<br>
<br>
Nmax=ndims(inArray);<br>
<br>
if N<Nmax<br>
<br>
tileSizes=[tileSizes,inf(1,NmaxN)];<br>
<br>
<br>
elseif N>Nmax <br>
<br>
tileSizes=tileSizes(1:Nmax);<br>
<br>
end<br>
N=Nmax;<br>
<br>
C=cell(1,N);<br>
<br>
for ii=1:N %loop over the dimensions<br>
<br>
dim=size(inArray,ii);<br>
T=min(dim, tileSizes(ii));<br>
<br>
if T~=floor(T)  T<=0<br>
error 'Tile dimension must be a strictly positive integer or Inf'<br>
end<br>
<br>
nn=( dim / T );<br>
nnf=floor(nn);<br>
<br>
resid=[];<br>
if nnf~=nn <br>
nn=nnf;<br>
resid=dimT*nn;<br>
end<br>
<br>
C{ii}=[ones(1,nn)*T,resid]; <br>
<br>
<br>
end<br>
<br>
outCell=mat2cell(inArray,C{:});

Sat, 04 Feb 2012 01:29:24 +0000
Re: Dividing a Matrix into submatrices
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316327#865750
Image Analyst
ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c8e60e6c8df640cf94743858ae9fba2c@c13g2000yqk.googlegroups.com>...<br>
> <br>
> m = rand(10)<br>
> <br>
> mCell = mat2cell(m, [2 2 2 2 2], [2 2 2 2 2])<br>
> <br>
> I trust you can figure out how to make up the [2 2 2 2 2] or [2 2 2]<br>
> vectors (in case you're using 6).<br>
<br>
Here's some general purpose code I wrote, with some example data and block sizes.<br>
<br>
% IMPORTANT: The newsreader may break long lines into multiple lines.<br>
% Be sure to join any long lines that got split into multiple single lines.<br>
% These can be found by the red lines on the left side of your<br>
% text editor, which indicate syntax errors, or else just run the <br>
% code and it will stop at the split lines with an error.<br>
<br>
% Divides a 2D matrix up into a certain number of nonoverlapping blocks (tiles).<br>
clc;<br>
<br>
% First create some sample data that we will divide up.<br>
% Sample matrix of 17 rows and 16 columns.<br>
% This option will have different sized blocks with the block sizes of 3 and 5.<br>
m = rand(17,16)<br>
% Sample matrix of 15 rows and 20 columns.<br>
% This option will have uniformly sized blocks with the block sizes of 3 and 5.<br>
m = rand(12, 15)<br>
% Modify the above lines to whatever you want or need.<br>
<br>
% Get the size of m<br>
[rows columns] = size(m);<br>
<br>
% Now specify the block sizes in the y (row) and x (column) directions.<br>
% For the rows, let's say that we want to specify<br>
% that the blocks are blockSizeY elements (rows) tall.<br>
blockSizeY = 3; % Say blockSizeY = 3 for example.<br>
% For the columns, let's say that we want to specify<br>
% that the blocks are blockSizeX elements (columns) wide.<br>
blockSizeX = 5; % Say blockSizeX = 5 for example.<br>
% Again, modify the above lines to whatever you want or need.<br>
<br>
% Find out how many times we can replicate this block size vertically<br>
% (down the rows) and still fit inside the array.<br>
numFullSizeBlocksY = floor(rows / blockSizeY)<br>
% Construct the block height array.<br>
blockHeights = blockSizeY * ones(numFullSizeBlocksY, 1)<br>
% Find out if there is a remaining smaller block that didn't fit.<br>
partialBlockY = rem(rows, blockSizeY)<br>
% If there is a smaller block, add it on to the block size array.<br>
if partialBlockY ~= 0<br>
blockHeights = [blockHeights; partialBlockY]<br>
end<br>
% Now we have our list of sizes for the blocks in the Y direction.<br>
<br>
% Now do the same thing for the other direction.<br>
% Find out how many times we can replicate this block size horizontally<br>
% (across the columns) and still fit inside the array.<br>
numFullSizeBlocksX = floor(columns / blockSizeX)<br>
% Construct the block width array.<br>
blockWidths = blockSizeX * ones(numFullSizeBlocksX, 1)<br>
% Find out if there is a remaining smaller block that didn't fit.<br>
partialBlockX = rem(columns, blockSizeX)<br>
% If there is a smaller block, add it on to the block size array.<br>
if partialBlockX ~= 0<br>
blockWidths = [blockWidths; partialBlockX]<br>
end<br>
% Now we have our list of sizes for the blocks in the X direction.<br>
<br>
% Now divide up the array. If we don't have uniformly sized blocks,<br>
% we can't use a 3D array. In that case we must use a cell array.<br>
% Even if the blocks are all the same size, <br>
% we can still use a cell array if that's what we want to do.<br>
mCell = mat2cell(m, blockHeights, blockWidths)<br>
% The cell array will be 6 rows by 4 columns for this example.<br>
fprintf('Made cell array of %d rows by %d columns.\n', ...<br>
length(blockHeights), length(blockWidths));<br>
% The arrays inside each cell will have a variety of sizes<br>
% since our blocks did not evenly divide the m array.<br>
<br>
% Optional: make 3D array if possible.<br>
if partialBlockX == 0 & partialBlockY == 0<br>
% All blocks are the same size.<br>
% We can put into a 3D array.<br>
% Determine the number of slices (planes) in the z direction.<br>
numberOfSlices = numel(mCell);<br>
% Preallocate space for the 3D array.<br>
m3D = zeros(blockSizeY, blockSizeX, numberOfSlices);<br>
% Do the assignment.<br>
for slice = 1 : numFullSizeBlocksY * numFullSizeBlocksX<br>
m3D(:, :, slice) = mCell{slice};<br>
end<br>
fprintf('Made 3D array of %d slices (planes).\n', numberOfSlices);<br>
end