MATLAB: Decompose a matrix into smaller matrices by the date

filtermatrixmatrix manipulationreshape

Dear all,
I have a matrix with the dimension 946836×3 and it is a cell. The first column is the date (dd.mm.yyyy 'HH:MM:SS), followed by two exchange rates. The rows look like this: '01.01.2004 00:00:00.000', 1.2587, 1.2698. The span of time goes from '01.01.2004 00:00:00.000' to '31.12.2012 23:55:00.000'. I have for every day intra-day data points(every 5 minutes, but they are not equal for every month, some have 288 and others 287 or 286). Now I want for every day a matrix filtered by the date. As an example in one matrix should be all datapoints of january 2004 and in the next for february and so. As a result I should have 12*9 matrices. How can I do that? Thank you in advance!

Best Answer

A few hints to help you start:
>> C = {'01.01.2004 18:23:54', 3, 4; ...
'02.01.2004 18:23:54', 2, 6; ...
'01.02.2004 18:23:54', 8, 7} ;
>> regexp(C(:,1), '01.2004')
ans =
[4]
[4]
[]
>> cellfun(@(x)~isempty(x), regexp(C(:,1), '01.2004'))
ans =
1
1
0
>> C(cellfun(@(x)~isempty(x), regexp(C(:,1), '01.2004')), 2:3)
ans =
[3] [4]
[2] [6]
>> m = 1 ; y = 2004 ;
>> sprintf('%.2d.%d', m ,y)
ans =
01.2004
>> C(cellfun(@(x)~isempty(x), regexp(C(:,1), sprintf('%.2d.%d', m ,y))), 2:3)
ans =
[3] [4]
[2] [6]
>> cell2mat(ans)
ans =
3 4
2 6
It is certainly not the most efficient way to do it, but it makes you "play a bit" with regexp, cellfun, etc. Note that as the month and the year are stored in numerical variables, you can easily loop over both of them. EDIT(2): e.g.
D = cell(9, 12) ; Cex = cell2mat(C(:,2:3)) ;
for yId = 1 : 9
for m = 1 : 12
D{yId,m} = Cex(cellfun(@(x)~isempty(x), regexp(C(:,1), ...
sprintf('%.2d.%d', m ,2003+yId))), :) ;
end
end
Hope it helps,
Cedric