Time intervals in a array
조회 수: 1 (최근 30일)
이전 댓글 표시
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.......
채택된 답변
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];
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!