MATLAB: How to take a really long array and analyze it in chunks without inputting how big the array is

sub matrix

I have an array that's 20000×1 and I want to break it down into smaller sections. I want to take those sections and group it with a standard array that I created. Then analyze that two column matrix and obtain some information. I then want to take all my results from each section and group them together in a table. I am using 2007b. I just need some advice and guidance on how to go about this efficiently. Thank you.

Best Answer

If the length of the section exactly divides the length of the vector, then reshape() the vector and work with the columns. For example,
A = rand(10000,1);
section_length = 250;
Ar = reshape(A, section_length, []);
then the columns of Ar would be 250 long.
This will not work if your section length does not exactly divide the length of the array, and will not work if you need overlaps between the sections.
If you need overlaps between the section, or the section length does not exactly divide, and you have the Signal Processing Toolbox, use buffer() to create the array.
Another way is to use mat2cell to divide up into chunks:
L = length(A);
chunk_lengths = section_length * ones(1, floor(L/section_length));
r = mod(L, section_length);
if r > 0
chunk_lengths(end+1) = r;
end
B = mat2cell(A(:), chunk_lengths, 1);
With this version, the last cell in B might be a different size, holding the "leftovers" after breaking up into even-sized chunks.