How to get number of occurrence in a list with gaps?

조회 수: 2 (최근 30일)
Redouane Ch
Redouane Ch 2019년 9월 22일
댓글: Bruno Luong 2019년 9월 22일
Good morning;
I have a list of dates like below:
1/1/2019
1/1/2019
2/1/2019
4/1/2019
4/1/2019
7/1/2019
7/1/2019
7/1/2019
I would like to generate a list with dates and number of occurrence. the output must be :
1/1/2019 2
2/1/2019 1
3/1/2019 0
4/1/2019 2
5/1/2019 0
6/1/2019 0
7/1/2019 3
I've been using "awk" to do that, but it doesn't give a good results with the "gaps" in the dates list
is there a way to do that with matlab?, I'm not very good at it.
  댓글 수: 2
madhan ravi
madhan ravi 2019년 9월 22일
편집: madhan ravi 2019년 9월 22일
Upload your data file. Which version of MATLAB are you using?
Redouane Ch
Redouane Ch 2019년 9월 22일
the file is attached, I'm using matlab 2016a

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

채택된 답변

Bruno Luong
Bruno Luong 2019년 9월 22일
편집: Bruno Luong 2019년 9월 22일
(EDIT for R2016a, not supported for strings.)
% dummy test data
d={'1/1/2019'
'1/1/2019'
'2/1/2019'
'4/1/2019'
'4/1/2019'
'7/1/2019'
'7/1/2019'
'7/1/2019'}
dn=datenum(d,'dd/mm/yyyy');
count=accumarray(dn-min(dn)+1,1);
date=datestr(min(dn)+(0:size(count,1)-1)','dd/mm/yyyy');
T=table(date,count)
returns result in table
T =
7×2 table
date count
__________ _____
01/01/2019 2
02/01/2019 1
03/01/2019 0
04/01/2019 2
05/01/2019 0
06/01/2019 0
07/01/2019 3
  댓글 수: 7
Redouane Ch
Redouane Ch 2019년 9월 22일
your suggestion works fine for me, but the results start from the first line of data.
if our file starts with "2/1/2019" it will not show "1/1/2019 0"
Bruno Luong
Bruno Luong 2019년 9월 22일
Yes that's how it supposes to work since 1/1/2019 is not belong to any gap.
Without any specification, how should the filling code decides to add counting from 1/1/2019 and not from 1/1/2000, or 1/1/1900?
If you want the code to makes the count from whatever the date, replace
min(dn)
occurences by
datenum('1/1/2019',...)

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

추가 답변 (0개)

카테고리

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