How to make 30min intervals from 1min data taking the previous value for NaNs?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ekaterina Serikova
el 8 de Mayo de 2016
Comentada: Ekaterina Serikova
el 10 de Mayo de 2016
Hello,
I have a matrix of type double(below).
It consists of 1min stock prices (StockA, StockB, etc.).
Year Mo Day Hour Min StockA StockB
2012 3 22 9 58 NaN 9.98
2012 3 22 9 59 NaN 9.40
2012 3 22 10 00 NaN NaN
2012 3 22 10 01 NaN 9.59
2012 3 22 10 02 NaN NaN
2012 3 22 10 03 10.03 9.91
2012 3 22 10 04 NaN NaN
I would like to aggregate the data in 30min intervals in such a way that if no price is available for exact 09.30,10.00, 10.30 and so on, it takes the last available value before 9.30,10.00, 10.30 and so on column by column.
Could you please advise how can I do that?
I tried to create 30mins intervals separately and use min(abs... function, but it seems not to work for the dates.
4 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 8 de Mayo de 2016
Editada: Azzi Abdelmalek
el 8 de Mayo de 2016
% -----------Example------------------------
v1=datenum('01-01-2016','dd-mm-yyyy');
pas=1/(24*60);
v=datestr(v1:pas:v1+pas*89);
n=size(v,1);
w=randi(10,n,2);
w(randperm(n*2,fix(3*n/2)))=nan;
A=[datevec(v) w];
%------------------------------------
m=size(A,2);
nn=30;
B=permute(reshape(A',m,nn,[]),[2 1 3]);
for k=1:size(B,3)
ii1=min([nn nn-find(~isnan(flipud(B(:,7,k))),1)+1])
ii2=min([nn nn-find(~isnan(flipud(B(:,8,k))),1)+1])
B(nn,7,k)=B(ii1,7,k)
B(nn,8,k)=B(ii2,8,k);
end
out=reshape(permute(B,[2 1 3]),size(A,2),size(A,1))'
6 comentarios
Más respuestas (1)
the cyclist
el 8 de Mayo de 2016
I think this does what you want. I've commented it to show the algorithm.
% Create some pretend prices. Use your actual price matrix here.
% Needs to be multiple of 30 in length along dimension 1.
% If it isn't, trim as needed.
originalPriceMatrix = reshape(1:240,60,4);
% Set last-minute price to NaN for testing
originalPriceMatrix(end,:) = NaN;
% Reshape to get each 30-minute segment along dimension 1
price = reshape(originalPriceMatrix,30,2,4);
% Carry forward price if NaN.
lastMinutePrices = price(1,:,:);
for nt = 2:30
thisMinutePrices = price(nt,:,:);
thisMinutePrices(isnan(thisMinutePrices)) = lastMinutePrices(isnan(thisMinutePrices));
lastMinutePrices = thisMinutePrices;
end
% Permute to get (time X stock)
lastMinutePrices = permute(lastMinutePrices,[2 3 1]);
1 comentario
Ekaterina Serikova
el 8 de Mayo de 2016
Editada: Ekaterina Serikova
el 8 de Mayo de 2016
Ver también
Categorías
Más información sobre Time Series Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!