No BSD License  

Highlights from
BLOCKFUN applies function on sub blocks of an array

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 3.44 KB File ID: #17000

BLOCKFUN applies function on sub blocks of an array

by Mathias Ortner

 

19 Oct 2007 (Updated 22 Oct 2007)

partition an ND-array into sublocks and then applies a function on each block

| Watch this File

File Information
Description

BLOCKFUN applies a function on blocks of an array
  Y=BLOCKFUN(X,S,funHandle) applies the function funHandle on blocks of size
  S on the array X. Y is of size ceil(size(X)./S)
  
  For instance, if X is of size [9 9] and S is [3 3], then
  X is partitionned in 9 [3 3] blocks as follow :
   | B1 | B4 | B7 |
   | B2 | B5 | B8 | where Bi's are [3 by 3] matrices
   | B3 | B6 | B9 |
  Y is then a [3 by 3] matrix with
   Y(i) = fun(Bi(:))
  
  This function is just a simple wrap for accumarray !
  See Also : accumarray
 
  EXAMPLES :
  rand('twister',12);
  x=floor(2*rand(9,9))
    x =
       0 0 1 1 1 0 0 0 0
       1 0 0 0 1 0 0 1 1
       0 1 1 1 0 0 0 0 0
       1 1 0 1 0 0 1 1 1
       0 1 0 0 0 0 0 0 0
       1 0 0 0 1 0 0 1 1
       1 1 0 1 0 1 1 1 0
       0 1 1 1 1 1 1 1 1
       1 0 0 1 0 0 0 0 1
 
  y=blockfun(x,[3 3],@sum)
  y =
       4 4 2
       4 2 5
       5 6 6
  
  y=blockfun(x,[3 3],@median)
  y =
       0 0 0
       0 0 1
       1 1 1
 
  y=blockfun(x,[5 5],@sum)
  y =
      12 5
      11 10
  
  x=floor(2*rand(6,6,6))
  y=blockfun(x,[3 2 3],@(x) length(find(x>0))>4)
 
  x=floor(2*rand(512,512,50));
  tic; y=blockfun(x,[5 5 5],@sum); toc

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
19 Oct 2007 urs (us) schwarz

good help with representative examples, nice error checks using the newly introduced ASSERTion routine, and an interesting, sleek computational engine WOULD make this contribution a very nice add-on - IF it wasn't for the enormous(!) amount of memory it needs during its intermediary steps (always of type double, even if the input is of a smaller data type), which - most unfortunately - makes this function almost unusable for even moderately big real-world problems, eg, a 3d set of mri scans of size
     512x512x20 (uint16)

us

22 Oct 2007 Mathias Ortner

I updated the file to take care of Us comments.
The author

Please login to add a comment or rating.
Updates
22 Oct 2007

I took us comment into account, and it now works for big arrays as well.

22 Oct 2007

Added another feature for automtic adressing int type selecting (it hopfully limits the amount of required memory)

22 Oct 2007

Optimized a line,

this new (and hopefully last) version is even faster than the previous one.

Tag Activity for this File
Tag Applied By Date/Time
matrices Mathias Ortner 22 Oct 2008 09:32:09
cellfun Mathias Ortner 22 Oct 2008 09:32:09
arrayfun Mathias Ortner 22 Oct 2008 09:32:09
block Mathias Ortner 22 Oct 2008 09:32:09

Contact us at files@mathworks.com