File Exchange

image thumbnail

mat2tiles(inArray,v​arargin)

version 1.0 (2.76 KB) by

Splits an array of any dimension into cell array of equal sized chunks.

30 Downloads

Updated

View License

MAT2TILES is basically a wrapper for mat2cell but with a more convenient interface when you are simply trying to decompose an N-dimensional array into equal-sized chunks. It takes the desired chunk-size as an input argument, whereas mat2cell does not. MAT2TILES also has some convenient shortcuts for when you only want to tile along particular dimensions (see below).
USAGE:
 
    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 dimension 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). This is useful if you want to tile along only certain array dimensions.
 
 EXAMPLE 1: Split a 28x28 matrix into 4x7 sub-matrices
 
      >> A=rand(28); C=mat2tiles(A,[4,7])
  
      C =
  
          [4x7 double] [4x7 double] [4x7 double] [4x7 double]
          [4x7 double] [4x7 double] [4x7 double] [4x7 double]
          [4x7 double] [4x7 double] [4x7 double] [4x7 double]
          [4x7 double] [4x7 double] [4x7 double] [4x7 double]
          [4x7 double] [4x7 double] [4x7 double] [4x7 double]
          [4x7 double] [4x7 double] [4x7 double] [4x7 double]
          [4x7 double] [4x7 double] [4x7 double] [4x7 double]
 
 EXAMPLE 2: Split a 20x20x6 array into 20x6x3 sub-arrays. This example
 illustrates how 'Inf' can be used to indicate that one of the sub-array
 dimensions is to be the same as in the original array, in this case size(A,1)=20.
 
 
      >> A=rand(20,20,6);
 
      >> C=mat2tiles(A,[Inf,6,3]) %equivalent to mat2tiles(A,[20,6,3])
  
      C(:,:,1) =
  
          [20x6x3 double] [20x6x3 double] [20x6x3 double] [20x2x3 double]
  
  
      C(:,:,2) =
  
          [20x6x3 double] [20x6x3 double] [20x6x3 double] [20x2x3 double]
 
 The example also shows a situation where the original array does not
 divide evenly into sub-arrays of the specified size. Note therefore that
 some boundary sub-chunks are 20x2x3.

Comments and Ratings (6)

Matt J

Matt J (view profile)

@Jonathan,

You would use cell2mat for that.

Is there a function for reversing this process after the data has been manipulated?

Matt J

Matt J (view profile)

Thanks, Chang!

Chang hsiung

I think it is a good entry, it is more intuitive than mat2cell.

Matt J

Matt J (view profile)

@Matthew
As mentioned in the Description section above, the routine is very similar in what it accomplishes to MATLAB's native mat2cell, differing only in its input syntax. Therefore, I didn't foresee the need for examples, which are already available in the mat2cell documentation. The input syntax for mat2tiles, you will find explained in its help comments

    C=mat2tiles(X,D1,D2,D3,...,Dn)
    C=mat2tiles(X,[D1,D2,D3,...,Dn])

As in mat2cell, X is a numeric input array and C is the same array partitioned into cells. The Di are the dimensions of the partitions.

Matthew

Interesting function and potentially very useful. However, neither your code nor its comments provide even a basic example that properly demonstrates its usage.

Updates

1.0

Added examples to help documentation.

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video