Time intervals in a array

조회 수: 1 (최근 30일)
Mate 2u
Mate 2u 2012년 2월 14일
Hi everybody. I have a N x 2 cell Matrix. The First column is the time stamp and the Second column is the price at that time.
Below is a sample of the first 5 entries of the Matrix. I need to compare this data to other data and hence I would need the time intervals to be constant. As you can see the price sometimes changes more than once WITHIN A SECOND.
Hence I need a program which would create the price for every second. NOTE:: 1) IF THERE ARE MORE THAN 1 PRICE CHANGE IN ONE SECOND THE PRICE IS THE LAST ON FOR THAT SECOND.
2)PRICE WOULD STAY THE SAME UNTIL IT CHANGES
Sample of Data
'''27/01/2012 16:30:32''' 58.8500000000000
'''27/01/2012 16:30:32''' 58.8800000000000
'''27/01/2012 16:30:32''' 58.8800000000000
'''27/01/2012 16:30:44''' 58.9400000000000
'''27/01/2012 17:07:06''' 59.0854000000000
'''27/01/2012 17:12:08''' 58.9575000000000
'''27/01/2012 17:17:12''' 58.9800000000000
OUTPUT REQUIRED
16:30:32 58.88
16:30:33 58.88
16:30:34 58.88
................
16:30:44 58.94
16:30:45 58.94
ETC.......
  댓글 수: 1
Mate 2u
Mate 2u 2012년 2월 14일
Additionally the time scale required is from 16:30:00 - 22:00:00

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 2월 14일
s = datevec('27/01/2012 16:30:00','dd/mm/yyyy HH:MM:SS');
k = int16(diff(datenum(['16:30:00';'22:00:00']))*24*3600);
outtime = datevec(datenum(cumsum([s;[zeros(k,5),ones(k,1)]])));
outtime = outtime(:,4:end);
data = {'27/01/2012 16:30:32' 58.85
'27/01/2012 16:30:32' 58.88
'27/01/2012 16:30:32' 58.88
'27/01/2012 16:30:44' 58.94
'27/01/2012 17:07:06' 59.085
'27/01/2012 17:12:08' 58.958
'27/01/2012 17:17:12' 58.98};
d1 = datevec(data(:,1),'dd/mm/yyyy HH:MM:SS');
d1 = d1(:,4:end);
[a b] = unique(d1,'rows');
i1 = cumsum(ismember(outtime,a,'rows'));
i2 = i1 ~= 0;
i3 = ones(size(i1));
i3(i2) = i1(i2);
d2out = cat(1,data{b(i3),2});
d2out(~i2) = 0;
out = [outtime d2out];
OR
s = datevec('27/01/2012 16:30:00','dd/mm/yyyy HH:MM:SS');
k = int16(diff(datenum(['16:30:00';'22:00:00']))*24*3600);
outtime = cellstr(datestr(datenum(cumsum([s;[zeros(k,5),ones(k,1)]])),'HH:MM:SS'));
data = {'27/01/2012 16:30:32' 58.85
'27/01/2012 16:30:32' 58.88
'27/01/2012 16:30:32' 58.88
'27/01/2012 16:30:44' 58.94
'27/01/2012 17:07:06' 59.085
'27/01/2012 17:12:08' 58.958
'27/01/2012 17:17:12' 58.98};
d1 = cellfun(@(x)x(numel(x) + (-7:0)),data(:,1),'un',0);
[a,b] = unique(d1);
i1 = cumsum(ismember(outtime,a));
i2 = i1 ~= 0;
i3 = ones(size(i1));
i3(i2) = i1(i2);
d2out = data(b(i3),2);
d2out(~i2) = {0};
out = [outtime d2out];
  댓글 수: 1
Mate 2u
Mate 2u 2012년 2월 14일
Great work

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by