- Store it as double matrix. You need to convert the dates, if in string format, to serial dates with datenum.
- Use logical indexing (no need of FOR loops)
- If you want to group data by date and id you may want to give a look at Pivot/UnPivot which I specifically use to work with panels of time series
EDIT
How data can be organized:
Amat = [repmat((now-2:now).',3,1),...
reshape(repmat(1:3,3,1),[],1),...
rand(9,1)];
Acel = [cellstr(datestr(Amat(:,1),'dd/mm/yyyy')),...
num2cell(Amat(:,2)),...
num2cell(Amat(:,3))];
Adat = dataset({Acel(:,1),'Date'},{Amat(:,2:3), 'Cocode','Rend'});
Memory used
whos
Name Size Bytes Class
Acel 9x3 1944 cell --> 1.5e6x3 ~ 308 mb
Adat 9x3 2114 dataset --> 1.5e6x3 ~ 336 mb
Amat 9x3 216 double --> 1.5e6x3 ~ 34 mb
Convert datestrings in serial dates for dataset and cell to save memory
Acel(:,1) = num2cell(datenum(Acel(:,1),'dd/mm/yyyy'));
Adat.Date = datenum(Adat.Date,'dd/mm/yyyy');
whos
Name Size Bytes Class
Acel 9x3 1836 cell
Adat 9x3 1466 dataset
Pros/cons:
- doule matrix: save on memory --> faster; cannot mix datatypes, so each date should be converted into serial numbers and doesn't have grpstats functionalities as the dataset class but should use accumarray or my Pivot/unPivot.
- dataset: easier to use and more friendly behavior (grpstats, stack/unstack etc...), can mix datatypes; consumes a LOT of memory
- cell array: can mix datatypes and consumes less memory than dataset; 'harder' to work with and still uses a LOT of memory
How to group data
Pivot(Amat(:,[2,1,3]))
NaN 1 2 3
7.3457e+005 0.54701 0.18896 0.36848
7.3457e+005 0.29632 0.68678 0.62562
7.3457e+005 0.74469 0.18351 0.78023
Pivot(Acel(:,[2,1,3]))
[ NaN] [ 1] [ 2] [ 3]
'06/03/2011' [0.54701] [0.18896] [0.36848]
'07/03/2011' [0.29632] [0.68678] [0.62562]
'08/03/2011' [0.74469] [0.18351] [0.78023]
unstack(Adat,'Rend','Cocode','AggregationFun',@sum)
Date x1 x2 x3
'06/03/2011' 0.54701 0.18896 0.36848
'07/03/2011' 0.29632 0.68678 0.62562
'08/03/2011' 0.74469 0.18351 0.78023
Oleg
Best Answer