How do I find the number of hours?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
stelios loizidis
el 21 de Jul. de 2021
Comentada: stelios loizidis
el 22 de Jul. de 2021
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.
0 comentarios
Respuesta aceptada
Steven Lord
el 21 de Jul. de 2021
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')
thisIsADuration = duration(12, 5, 0, 'Format', 'hh:mm')
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))
timeSinceMidnight = dt - dateshift(dt, 'start', 'day')
These two histogram plots look very different, since one is binning by hours and one by days.
histogram(timeSinceMidnight, 6)
histogram(dt, 6)
0 comentarios
Más respuestas (2)
Mudit Chaturvedi
el 21 de Jul. de 2021
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)];
0 comentarios
stelios loizidis
el 21 de Jul. de 2021
4 comentarios
Steven Lord
el 21 de Jul. de 2021
Use a vector of bin edges.
dt = datetime('now') + hours(24*rand(10, 1))
timeSinceMidnight = dt - dateshift(dt, 'start', 'day')
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.
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!