2.33333

2.3 | 3 ratings Rate this file 3 Downloads (last 30 days) File Size: 1.47 KB File ID: #10004

chunkify

by Gerald Dalley

 

15 Feb 2006 (Updated 21 Feb 2006)

Splits a vector or cell array into evenly-sized chunks.

| Watch this File

File Information
Description

C = CHUNKIFY(DATA, CHUNKSIZE)

Turns a matrix DATA into chunks of data with CHUNKSIZE elements in each cell entry of C. If DATA's size is not evenly divisible by CHUNKSIZE, the last entry in C will contain fewer items.

Examples:

chunkify([1 2 3 4], 2) --> {[1 2] [3 4]}
chunkify([1 2 3 4 5], 2) --> {[1 2] [3 4] [5]}

chunkify({'1' '2' '3' '4'}, 2) --> {{'1' '2'} {'3' '4'}}

Motivating scenario:

I wrote this function because I needed to process a lot of data in separate chunks, sometimes to have different machines run each chunk, sometimes to just load in chunks of the data files so I could balance I/O and memory constraints. In both these cases, I found it handy to have this simple function to break up my index vector. Because the number of data items was not always an exact multiple of my desired chunk size, I used this function instead of Matlab's built-in RESHAPE (putting one chunk per row, for example).

TODO:
If DATA is multi-dimensional, the dimension on which to chunkify is given by DIM.
   chunkify([1 2 3 4], 2, 2) --> {[1 2] [3 4]}
   chunkify([1 2 3 4], 2, 1) --> {[1 2 3 4]}
   chunkify([1 2; 3 4], 2, 1) --> {[1 2] [3 4]}

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
16 Feb 2006 S. Bart

Another piece of junk, and guess what this time: this junky chunky code has a Copyright!

17 Feb 2006 Duane Hanselman

Why would someone want to do this so often that one needs copyrighted code to do it? The simplicity and limited functionality of this code gives it dubious utility. If the copyright notice and permission notice take up more bytes that the code, one has to question either the copyright or the code or both. I'm not seeing something important and useful enough here to justify sharing it with others.

18 Feb 2006 Gerald Dalley

I decided to remove the boilerplate copyright and add a motivating scenario. Use this function if you think it's useful. If not, don't. The new version should be up by 22 Feb or earlier.

24 Feb 2006 John D'Errico

Good help, explaining clearly when to use this tool. No boilerplate anymore. I'll be honest, before you added the section explaining its use, I had absolutely no idea where one might find a use for this tool. That helped a lot. And, since its a task that I do fairly often, even though its not hard to do, I like it. Thanks for fixing it up.

19 Jan 2008 Micah Richert

You can do this in one line using accumarray, and it easily solves the multi-dimension issue:
accumarray([floor((0:numel(DATA)-1)/CHUNKSIZE)'+1 ones(5,1)],DATA,[],@(x) {x})

Please login to add a comment or rating.
Updates
21 Feb 2006

Removed boilerplate copyright, added motivating example.

Tag Activity for this File
Tag Applied By Date/Time
structures Gerald Dalley 22 Oct 2008 08:15:54
cell arrays Gerald Dalley 22 Oct 2008 08:15:54
matrix Cristina McIntire 23 Feb 2009 15:34:28
cell Cristina McIntire 23 Feb 2009 15:34:28
chunk Cristina McIntire 23 Feb 2009 15:34:28
split Cristina McIntire 23 Feb 2009 15:34:28
segment Cristina McIntire 23 Feb 2009 15:34:28

Contact us at files@mathworks.com