MATLAB: How to order data according to month

datamatrices

I have been thinking about a proper way to do this and thought I would ask for input here:
I have 8 years with data, these are 1x1000x365 matrices (for every day I have 1000 measures, on leap years february the 29th is ignored)
I want to sort this by month, so I want twelve (days of the month * 8)x1000 matrices.
The matrices are just filled with numbers, nothing special (Matlab calls it 'double').
Any thoughts on how to do this best in Matlab?

Best Answer

Assuming your yearly data is stored in a cell array called yearlydata, if it is stored in individual matrices, you can build the cell array with:
yearlydata = {year1, year2, year3, ... };
and assuming that your yearly matrices are 1000x365 (if they are indeed 1x1000x365, you can use squeeze to remove the first singleton dimension). So, the days are the columns.
1. You want to interleave the data so that the first n (n = number of years) are day1, the next n columns are day2, etc. A combination of cat, reshape and permute would do it
numberofyears = numel(yearlydata); %should be 8
numberofmeasures = size(yearlydata{1}, 1); %should be 1000
alldata = cat(3, yearlydata{:});
alldata = reshape(permute(alldata, [1 3 2]), numberofmeasures, [], 1);
2. You can then use mat2cell to split the columns by month:
daypermonth = [31 28 31 30 31 30 31 31 30 31 30 31];
monthlydata = mat2cell(alldata, numberofmeasures, daypermonth .* numberofyears);