From: Ignazio G on 7 Jul 2010 12:05 I've got the same problem: I have to average data (qj) corresponding to the same day (I deal with a varying number of records per day): yr month day qj 1968 11 13 139.5 1968 11 13 139.5 1968 11 13 138.4 1968 11 13 138.6 1968 11 14 131.4 1968 11 14 129.8 1968 11 15 132.1 1968 11 15 131 1968 11 15 131 In this case I have to average 4 qj values for day 13 2 for day 14 and 3 for day 15. As suggested before I have transformed the date into serial date numbers using 'datenum': N = DATENUM(yr,month,day); then I got the unique values of such vector: U = unique(N); qjA = zeros(length(U),1); % qj averaged vector but the following step doesn't seem to work: for i=1:length(U) qjA(i) = mean(qj(U(i),1)); % the average for each day end I get this error: "??? Index exceeds matrix dimensions." inside the for loop ! ! How can I succeed? or is there another way to solve this? thank you in advance! > No real time at moment but look at > > datenum() > unique() > > "logical addressing" > > In summary, turn the first column into serial date numbers via datenum, > find the unique days w/ unique and then use logical addressing w/ that > result as the index in the mean() function. > > As for the column, x(:,2) would be simply the response values above. > > d = datenum(x(:,1)); % get the date value > u = unique(d); % a list of all the unique days in the dataset > for i=1:length(u) > a(i) = mean(x(u(i),2)); % the average for each day > end > > To do the time periods simply concatenate the time onto the day value > (again w/ datenum) and expand the selection logic to filter on the > appropriate fractional values. > > --
From: Oleg Komarov on 7 Jul 2010 13:57 "Ignazio G" <igiuntoli(a)yahoo.it> wrote in message <i128k0$omj$1(a)fred.mathworks.com>... > I've got the same problem: > > I have to average data (qj) corresponding to the same day (I deal with a varying number of records per day): > yr month day qj > 1968 11 13 139.5 > 1968 11 13 139.5 > 1968 11 13 138.4 > 1968 11 13 138.6 > 1968 11 14 131.4 > 1968 11 14 129.8 > 1968 11 15 132.1 > 1968 11 15 131 > 1968 11 15 131 > > In this case I have to average 4 qj values for day 13 > 2 for day 14 and 3 for day 15. > > As suggested before I have transformed the date into serial date numbers using 'datenum': > N = DATENUM(yr,month,day); > then I got the unique values of such vector: > U = unique(N); > > qjA = zeros(length(U),1); % qj averaged vector > > but the following step doesn't seem to work: > for i=1:length(U) > qjA(i) = mean(qj(U(i),1)); % the average for each day > end > > I get this error: "??? Index exceeds matrix dimensions." > inside the for loop ! ! > > How can I succeed? > or is there another way to solve this? > > thank you in advance! > > > No real time at moment but look at > > > > datenum() > > unique() > > > > "logical addressing" > > > > In summary, turn the first column into serial date numbers via datenum, > > find the unique days w/ unique and then use logical addressing w/ that > > result as the index in the mean() function. > > > > As for the column, x(:,2) would be simply the response values above. > > > > d = datenum(x(:,1)); % get the date value > > u = unique(d); % a list of all the unique days in the dataset > > for i=1:length(u) > > a(i) = mean(x(u(i),2)); % the average for each day > > end > > > > To do the time periods simply concatenate the time onto the day value > > (again w/ datenum) and expand the selection logic to filter on the > > appropriate fractional values. > > > > -- @Vinny % You have: In = {'3/14/75' 2 '3/14/75' 4 '3/14/75' 2 '3/15/75' 3 '3/15/75' 4 '3/15/75' 3}; % Adapt input for Pivot In = [num2cell(ones(size(In,1),1)) In]; % Use Pivot Pivot(In,@mean) ans = [ NaN] [1.00] '3/14/75' [2.67] '3/15/75' [3.33] @Ignazio G % You have: In = [1968 11 13 139.5 1968 11 13 139.5 1968 11 13 138.4 1968 11 13 138.6 1968 11 14 131.4 1968 11 14 129.8 1968 11 15 132.1 1968 11 15 131 1968 11 15 131 ]; % Adapt input for Pivot In = [In(:,1)*100 + In(:,2) In(:,3:4)]; % Use Pivot Pivot(In,@mean) ans = NaN 196811.00 13.00 139.00 14.00 130.60 15.00 131.37 You can find Pivot on the FEX: http://www.mathworks.com/matlabcentral/fileexchange/26119 Oleg
From: Ignazio G on 8 Jul 2010 05:46 Thank you Oleg, but I am using matlab 6 and the 'pivot' module is not compatible. Matlab 6 lacks the 'accumarray' function. is there any other way?! > @Ignazio G > > % You have: > In = [1968 11 13 139.5 > 1968 11 13 139.5 > 1968 11 13 138.4 > 1968 11 13 138.6 > 1968 11 14 131.4 > 1968 11 14 129.8 > 1968 11 15 132.1 > 1968 11 15 131 > 1968 11 15 131 ]; > > % Adapt input for Pivot > In = [In(:,1)*100 + In(:,2) In(:,3:4)]; > > % Use Pivot > Pivot(In,@mean) > ans = > NaN 196811.00 > 13.00 139.00 > 14.00 130.60 > 15.00 131.37 > > You can find Pivot on the FEX: http://www.mathworks.com/matlabcentral/fileexchange/26119 > > Oleg
From: Oleg Komarov on 8 Jul 2010 06:18 "Ignazio G" <igiuntoli(a)yahoo.it> wrote in message <i146or$6tm$1(a)fred.mathworks.com>... > Thank you Oleg, but I am using matlab 6 and the 'pivot' module is not compatible. Matlab 6 lacks the 'accumarray' function. > > is there any other way?! > In = [1968 11 13 139.5 1968 11 13 139.5 1968 11 13 138.4 1968 11 13 138.6 1968 11 14 131.4 1968 11 14 129.8 1968 11 15 132.1 1968 11 15 131 1968 11 15 131 ]; % Serial dates N = datenum(In(:,1:3)); % Unique dates and index [unDates,trash,IDX] = unique(N); %Preallocate output Out = [unDates zeros(numel(unDates),1)]; % Averaging for d = 1:numel(unDates) Out(d,2) = mean(In(IDX == d,end)); end Out = 719115.00 139.00 719116.00 130.60 719117.00 131.37 Oleg
From: Ignazio G on 8 Jul 2010 09:13 Thank you very much, that's exactly what I wanted to do! My last question: is there a way to get the date back from datenum format to 'year month day'? > In = [1968 11 13 139.5 > 1968 11 13 139.5 > 1968 11 13 138.4 > 1968 11 13 138.6 > 1968 11 14 131.4 > 1968 11 14 129.8 > 1968 11 15 132.1 > 1968 11 15 131 > 1968 11 15 131 ]; > > % Serial dates > N = datenum(In(:,1:3)); > % Unique dates and index > [unDates,trash,IDX] = unique(N); > > %Preallocate output > Out = [unDates zeros(numel(unDates),1)]; > % Averaging > for d = 1:numel(unDates) > Out(d,2) = mean(In(IDX == d,end)); > end > > Out = > 719115.00 139.00 > 719116.00 130.60 > 719117.00 131.37 > > Oleg
|
Next
|
Last
Pages: 1 2 Prev: Figures opening as logical variables Next: Opinions on 'resample' function? |