How do I find the number of hours?

조회 수: 13 (최근 30일)
stelios loizidis
stelios loizidis 2021년 7월 21일
댓글: stelios loizidis 2021년 7월 22일
Hello,
I have a matrix A with dimensions 35000X1 (datetime). There are hours in matrix A.
For example, 01:00, 05:00, 17:00, 23:00, 05:00, 09:00, ....
How can I find the number of each hour in matrix A. That is, how many hours are there that correspond to 00:00, 01:00,02:00,03:00, .... 21:00,22:00,23:00. Your help is important.

채택된 답변

Steven Lord
Steven Lord 2021년 7월 21일
I have a matrix A with dimensions 35000X1 (datetime). There are hours in matrix A.
For example, 01:00, 05:00, 17:00, 23:00, 05:00, 09:00, ....
Are these a datetime array or a duration array?
thisIsADatetime = datetime('now', 'Format', 'HH:mm')
thisIsADatetime = datetime
12:05
thisIsADuration = duration(12, 5, 0, 'Format', 'hh:mm')
thisIsADuration = duration
12:05
These are not the same, despite looking the same. The thisIsADatetime array has a date associated with it, while thisIsADuration does not. If it's a datetime array you probably want to calculate the time since midnight and compute histcounts on the resulting duration array.
dt = datetime('now') + days(randi([-2 2], 10, 1)) + hours(2*randn(10, 1))
dt = 10×1 datetime array
23-Jul-2021 11:09:17 20-Jul-2021 08:23:33 22-Jul-2021 12:42:30 21-Jul-2021 12:43:33 19-Jul-2021 14:33:25 21-Jul-2021 13:13:11 20-Jul-2021 12:20:07 19-Jul-2021 11:25:52 23-Jul-2021 13:16:16 20-Jul-2021 11:35:16
timeSinceMidnight = dt - dateshift(dt, 'start', 'day')
timeSinceMidnight = 10×1 duration array
11:09:17 08:23:33 12:42:30 12:43:33 14:33:25 13:13:11 12:20:07 11:25:52 13:16:16 11:35:16
These two histogram plots look very different, since one is binning by hours and one by days.
histogram(timeSinceMidnight, 6)
histogram(dt, 6)

추가 답변 (2개)

Mudit Chaturvedi
Mudit Chaturvedi 2021년 7월 21일
Hi!
I understand you are trying to find the frequency of each element in a matrix.
You can use unique() function to get the unique elements and then calculate their frequency using histc or histcounts function
a = unique(A);
sol = [a,histc(A(:),a)];
Please refer to this Matlab Answers link for more information.

stelios loizidis
stelios loizidis 2021년 7월 21일
Another question I have is if I have table A but the available data are as follows: 1/2/2014 01:00, 4/5/2014 05:00, 6/8/2014 16:00,... How can I calculate the frequency that the hours appear regardless of the date.
  댓글 수: 4
Steven Lord
Steven Lord 2021년 7월 21일
Use a vector of bin edges.
dt = datetime('now') + hours(24*rand(10, 1))
dt = 10×1 datetime array
22-Jul-2021 00:35:06 22-Jul-2021 04:14:00 21-Jul-2021 23:30:36 22-Jul-2021 03:32:47 22-Jul-2021 09:54:42 22-Jul-2021 02:09:19 21-Jul-2021 18:09:34 21-Jul-2021 18:48:10 22-Jul-2021 07:20:03 21-Jul-2021 18:17:55
timeSinceMidnight = dt - dateshift(dt, 'start', 'day')
timeSinceMidnight = 10×1 duration array
00:35:06 04:14:00 23:30:36 03:32:47 09:54:42 02:09:19 18:09:34 18:48:10 07:20:03 18:17:55
histogram(timeSinceMidnight, hours(0:24))
xticks(hours(0:24))
You might want to only display every other tick or change the Format property of the duration array that you pass into xticks.
stelios loizidis
stelios loizidis 2021년 7월 22일
Thanks for the valuable help !!!!

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

카테고리

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