Counting years from a date time variable

조회 수: 3 (최근 30일)
Luca
Luca 2021년 5월 15일
댓글: Luca 2021년 5월 16일
Hi,
I have table with a datetime variable in it from 02.January.1962 to 31.December 2016. i would like to create a new you column where the value staring with one changes every 5 year.
So the periond from 02.January.1962 - 31.December. 1966 would be one the next five year would be 2 an so on.
Does someone know how to do it?
Thank you in advance.

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 5월 15일
편집: Scott MacKenzie 2021년 5월 15일
This seems to achieve what you are after...
% test data
d = datetime(randi([1962 2020],1,50), randi(12,1,50), randi(28,1,50));
d = sort(d);
yrs = d(:).Year;
span5 = yrs(1):5:yrs(end)+5;
n = discretize(yrs, span5);
T = timetable(d', n)
Output:
T =
50×1 timetable
Time n
___________ __
04-Sep-1962 1
13-Nov-1962 1
25-Apr-1964 1
11-Jun-1965 1
26-Jul-1965 1
08-Sep-1965 1
10-Mar-1967 2
14-Feb-1968 2
13-Mar-1968 2
05-Feb-1969 2
18-Feb-1970 2
08-Mar-1971 2
12-Feb-1974 3
22-Jan-1975 3
10-Aug-1975 3
12-Sep-1976 3
04-Mar-1977 4
17-Apr-1977 4
23-Dec-1979 4
19-Jul-1981 4
08-Apr-1983 5
14-Feb-1984 5
15-Aug-1984 5
19-Feb-1986 5
20-Jun-1987 6
27-Oct-1989 6
28-Mar-1992 7
11-Jan-1993 7
18-Jan-1993 7
06-Oct-1995 7
25-Mar-1996 7
14-Oct-1996 7
07-Feb-1998 8
12-Sep-1998 8
14-Oct-1998 8
01-Jan-2001 8
10-May-2003 9
09-Aug-2004 9
08-Apr-2006 9
23-Aug-2006 9
08-Nov-2007 10
28-Feb-2010 10
08-Mar-2010 10
26-Aug-2012 11
18-Aug-2013 11
20-Apr-2014 11
15-Oct-2014 11
03-Feb-2015 11
21-Jun-2016 11
20-Oct-2017 12

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by