How can i change the precision using datenum?

조회 수: 3 (최근 30일)
André Fernandes
André Fernandes 2018년 10월 29일
댓글: Peter Perkins 2018년 10월 31일
Im using datenum on some datasets to try to see the intersection between the data (using intersect function), the problem is that im getting different times with the same time (the last few numbers are a little bit different). I already saw some people using a different aproach but i don't know how can i do it with my data. Im posting the code im using:
horas = datestr(horas);
datas = datestr(datas);
timesteps = [num2str(datas) num2str(horas(:,12:17))];
timesteps = datenum(timesteps,'dd-mmm-yyyy HH:MM');
[c,ia,ib]=intersect(timesteps,time_interval);
hm0(ib) = hm0;
It should work but since it's not getting all the times, it fails and tells me "In an assignment A(:) = B, the number of elements in A and B must be the same.". But if i can solve the early problem it will work since i already used this function before. Thank you
  댓글 수: 1
Stephen23
Stephen23 2018년 10월 29일

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

채택된 답변

Stephen23
Stephen23 2018년 10월 29일
편집: Stephen23 2018년 10월 29일
The basic problem is that intersect tests for exact equivalency of floating point numbers (which is all that datenum's are). This should be avoided, for reasons that have been discussed thousands of times before (see the link in my comment). Here are some proposals for how to deal with this:
  • Round the datenum to some number of decimal places. This sounds simple, but it can introduce artifacts into the data, and requires picking an appropriate number of digits to round to. A bit fiddly.
  • Download and use my FEX submission dateround, which rounds to the nearest second, minute, hour, etc.
  • Convert to datevec, pick the units/column you want to use (e.g. years to minutes), and then use intersect with the 'rows' option.
Personally I prefer the last solution: neat, intuitive, and does not involve comparing floating point numbers. Here is a complete working example, which demonstrates how differences in the seconds can be ignored:
>> C1 = {'2018-10-29 12:34:56','2018-10-29 12:00:00'};
>> C2 = {'2018-10-29 12:34:55','2018-10-29 12:01:00'};
>> V1 = datevec(C1(:))
V1 =
2018 10 29 12 34 56
2018 10 29 12 0 0
>> V2 = datevec(C2(:))
V2 =
2018 10 29 12 34 55
2018 10 29 12 1 0
>> intersect(V1(:,1:5),V2(:,1:5),'rows')
ans =
2018 10 29 12 34
Note that this is equivalent to doing a floor operation on the seconds column: to round the seconds up/down to the nearest minute you could either use dateround or do the rounding yourself.
  댓글 수: 2
André Fernandes
André Fernandes 2018년 10월 29일
Thank you very much Stephen. I used the last solution and it seems to be working :) I will try the other solutions aswell!
Stephen23
Stephen23 2018년 10월 29일
@André Fernandes: I hope that it helps. You might also like to consider using datetime objects, which can probably be rounded to particular units and compared.

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

추가 답변 (1개)

André Fernandes
André Fernandes 2018년 10월 29일
편집: Stephen23 2018년 10월 29일
Yes, i used:
time_interval = datevec(time_interval);
timesteps = datevec(timesteps);
[c,ia,ib]=intersect(timesteps(:,1:5),time_interval(:,1:5),'rows','stable');
hm0(ib) = hm0; %went to get the variable i wanted.
Im having a problem cus since this is inside a loop and the timeseries change everystep, in an interation where he just get 18 intersections then my hm0(ib) = hm0 gives me an error again "In an assignment A(:) = B, the number of elements in A and B must be the same."
  댓글 수: 6
André Fernandes
André Fernandes 2018년 10월 29일
Alright, going to try it out after lunch. Thank you very much Stephen, ur helping alot!
Peter Perkins
Peter Perkins 2018년 10월 31일
This, from Stephen,
"Use datetime objects and tables to group the data within the required period."
is the correct advice.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by