How to order data according to month?

9 views (last 30 days)
Evelyn
Evelyn on 12 Jan 2015
Commented: Evelyn on 12 Jan 2015
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?

Accepted Answer

Guillaume
Guillaume on 12 Jan 2015
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);
  3 Comments
Guillaume
Guillaume on 12 Jan 2015
I'm not changing the number of rows, so it should be equal to the number of measures per day. So yes, alldata is a 1000 x (365x8) matrix. The first 8 columns are the first day of each year, etc.
mat2cell then split these columns according to the numbers of days in the month x number of years. As a results, the output is a cell array of 12 matrices of size 1000 x (days of the month * 8). Exactly as you asked, except maybe transposed.
Now, if instead you want to concatenate vertically all the measures of the same day of each year, you'd do:
alldata = cat(3, yearlydata{:});
alldata = reshape(permute(alldata, [1 3 2]), numberofmeasures * numberofyears, [], 1);
In which case alldata is indeed a 8000 x 365 matrix. To then convert that in a monthly matrix:
daypermonth = [31 28 31 30 31 30 31 31 30 31 30 31];
monthlydata = mat2cell(alldata, numberofmeasures * numberofyears, daypermonth);
And you get 12 matrices of size with 8000 x days of the month.
Either way, the principle is the same, reshape the matrix to the desired form and split using mat2cell.
Evelyn
Evelyn on 12 Jan 2015
I understand, thank you so much!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!