Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Dividing a Matrix into submatrices

Subject: Dividing a Matrix into submatrices

From: Kevin Ellis

Date: 27 Jan, 2012 04:20:10

Message: 1 of 6

Hello,

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?

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)?

Any help would be much appreciated. Thanks.

Kevin

Subject: Dividing a Matrix into submatrices

From: ImageAnalyst

Date: 27 Jan, 2012 06:09:47

Message: 2 of 6

On Jan 26, 8:20 pm, "Kevin Ellis" <kevin.elli...@gmail.com> wrote:
> Hello,
>
> 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?
>
> 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)?
>
> Any help would be much appreciated. Thanks.
>
> Kevin

---------------------------------------------------------------------------
m = rand(10)

mCell = mat2cell(m, [2 2 2 2 2], [2 2 2 2 2])

I trust you can figure out how to make up the [2 2 2 2 2] or [2 2 2]
vectors (in case you're using 6).

Subject: Dividing a Matrix into submatrices

From: Kevin Ellis

Date: 27 Jan, 2012 06:18:16

Message: 3 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c8e60e6c-8df6-40cf-9474-3858ae9fba2c@c13g2000yqk.googlegroups.com>...
> On Jan 26, 8:20 pm, "Kevin Ellis" <kevin.elli...@gmail.com> wrote:
> > Hello,
> >
> > 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?
> >
> > 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)?
> >
> > Any help would be much appreciated. Thanks.
> >
> > Kevin
>
> ---------------------------------------------------------------------------
> m = rand(10)
>
> mCell = mat2cell(m, [2 2 2 2 2], [2 2 2 2 2])
>
> I trust you can figure out how to make up the [2 2 2 2 2] or [2 2 2]
> vectors (in case you're using 6).

Okay. Great. Thanks for the help.

Subject: Dividing a Matrix into submatrices

From: Steven_Lord

Date: 27 Jan, 2012 14:27:53

Message: 4 of 6



"Kevin Ellis" <kevin.ellis86@gmail.com> wrote in message
news:jft8lq$3n6$1@newscl01ah.mathworks.com...
> Hello,
>
> 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.

25, actually.

> How would I set up the command for mat2cell to accomplish this?

ImageAnalyst already answered this, but if you're looking to evaluate a
function on each of those blocks and you have Image Processing Toolbox, look
at the BLOCKPROC (or for older versions, BLKPROC) function.

http://www.mathworks.com/help/toolbox/images/ref/blockproc.html

It will handle splitting your matrix into submatrices; you won't need to
worry about MAT2CELL.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Dividing a Matrix into submatrices

From: Matt J

Date: 27 Jan, 2012 18:16:09

Message: 5 of 6

"Kevin Ellis" wrote in message <jft8lq$3n6$1@newscl01ah.mathworks.com>...
> Hello,
>
> 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?
>
> 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)?
====================

When the sub-matrices are all the same size, as in your case, I find it more convenient to use the MAT2TILES function below, e.g.,

>> mat2tiles(rand(10),[2,2])

ans =

    [2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]
    [2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]
    [2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]
    [2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]
    [2x2 double] [2x2 double] [2x2 double] [2x2 double] [2x2 double]

>> mat2tiles(rand(6),[2,2])

ans =

    [2x2 double] [2x2 double] [2x2 double]
    [2x2 double] [2x2 double] [2x2 double]
    [2x2 double] [2x2 double] [2x2 double]



function outCell=mat2tiles(inArray,varargin)
%MAT2TILES - breaks up an array into a cell array of adjacent sub-arrays of
% equal sizes
%
% C=mat2tiles(X,D1,D2,D3,...,Dn)
% C=mat2tiles(X,[D1,D2,D3,...,Dn])
%
%will produce a cell array C containing adjacent chunks of the array X,
%with each chunk of dimensions D1xD2xD3x...xDn. If a dimensions Di does
%not divide evenly into size(X,i), then the chunks at the upper boundary of
%X along dimension i will be truncated.
%
%It is permissible for the Di to be given value Inf. When this is done, it is
%equivalent to setting Di=size(X,i).
%
%If n < ndims(X), then the unspecified dimensions Dj, n<j<=ndims(X) will be
%set to size(X,i).
%
%If n > ndims(X), the the extra dimensions Dj, j>ndims(X) will be ignored.

tileSizes=[varargin{:}];

N=length(tileSizes);

Nmax=ndims(inArray);

if N<Nmax
   
    tileSizes=[tileSizes,inf(1,Nmax-N)];
    
    
elseif N>Nmax
    
    tileSizes=tileSizes(1:Nmax);
    
end
N=Nmax;

C=cell(1,N);

for ii=1:N %loop over the dimensions
    
 dim=size(inArray,ii);
 T=min(dim, tileSizes(ii));
 
 if T~=floor(T) || T<=0
     error 'Tile dimension must be a strictly positive integer or Inf'
 end
 
 nn=( dim / T );
   nnf=floor(nn);
 
 resid=[];
 if nnf~=nn
    nn=nnf;
    resid=dim-T*nn;
 end
 
 C{ii}=[ones(1,nn)*T,resid];
    
    
end

outCell=mat2cell(inArray,C{:});

Subject: Dividing a Matrix into submatrices

From: Image Analyst

Date: 4 Feb, 2012 01:29:24

Message: 6 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c8e60e6c-8df6-40cf-9474-3858ae9fba2c@c13g2000yqk.googlegroups.com>...
> ---------------------------------------------------------------------------
> m = rand(10)
>
> mCell = mat2cell(m, [2 2 2 2 2], [2 2 2 2 2])
>
> I trust you can figure out how to make up the [2 2 2 2 2] or [2 2 2]
> vectors (in case you're using 6).
--------------------------------------------------------------------------------------
Here's some general purpose code I wrote, with some example data and block sizes.

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% Divides a 2D matrix up into a certain number of non-overlapping blocks (tiles).
clc;

% First create some sample data that we will divide up.
% Sample matrix of 17 rows and 16 columns.
% This option will have different sized blocks with the block sizes of 3 and 5.
m = rand(17,16)
% Sample matrix of 15 rows and 20 columns.
% This option will have uniformly sized blocks with the block sizes of 3 and 5.
m = rand(12, 15)
% Modify the above lines to whatever you want or need.

% Get the size of m
[rows columns] = size(m);

% Now specify the block sizes in the y (row) and x (column) directions.
% For the rows, let's say that we want to specify
% that the blocks are blockSizeY elements (rows) tall.
blockSizeY = 3; % Say blockSizeY = 3 for example.
% For the columns, let's say that we want to specify
% that the blocks are blockSizeX elements (columns) wide.
blockSizeX = 5; % Say blockSizeX = 5 for example.
% Again, modify the above lines to whatever you want or need.

% Find out how many times we can replicate this block size vertically
% (down the rows) and still fit inside the array.
numFullSizeBlocksY = floor(rows / blockSizeY)
% Construct the block height array.
blockHeights = blockSizeY * ones(numFullSizeBlocksY, 1)
% Find out if there is a remaining smaller block that didn't fit.
partialBlockY = rem(rows, blockSizeY)
% If there is a smaller block, add it on to the block size array.
if partialBlockY ~= 0
    blockHeights = [blockHeights; partialBlockY]
end
% Now we have our list of sizes for the blocks in the Y direction.

% Now do the same thing for the other direction.
% Find out how many times we can replicate this block size horizontally
% (across the columns) and still fit inside the array.
numFullSizeBlocksX = floor(columns / blockSizeX)
% Construct the block width array.
blockWidths = blockSizeX * ones(numFullSizeBlocksX, 1)
% Find out if there is a remaining smaller block that didn't fit.
partialBlockX = rem(columns, blockSizeX)
% If there is a smaller block, add it on to the block size array.
if partialBlockX ~= 0
    blockWidths = [blockWidths; partialBlockX]
end
% Now we have our list of sizes for the blocks in the X direction.

% Now divide up the array. If we don't have uniformly sized blocks,
% we can't use a 3D array. In that case we must use a cell array.
% Even if the blocks are all the same size,
% we can still use a cell array if that's what we want to do.
mCell = mat2cell(m, blockHeights, blockWidths)
% The cell array will be 6 rows by 4 columns for this example.
fprintf('Made cell array of %d rows by %d columns.\n', ...
    length(blockHeights), length(blockWidths));
% The arrays inside each cell will have a variety of sizes
% since our blocks did not evenly divide the m array.

% Optional: make 3D array if possible.
if partialBlockX == 0 & partialBlockY == 0
    % All blocks are the same size.
    % We can put into a 3D array.
    % Determine the number of slices (planes) in the z direction.
    numberOfSlices = numel(mCell);
    % Preallocate space for the 3D array.
    m3D = zeros(blockSizeY, blockSizeX, numberOfSlices);
    % Do the assignment.
    for slice = 1 : numFullSizeBlocksY * numFullSizeBlocksX
        m3D(:, :, slice) = mCell{slice};
    end
    fprintf('Made 3D array of %d slices (planes).\n', numberOfSlices);
end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us