필터 지우기
필터 지우기

Average of consecutively increasing numbers

조회 수: 1 (최근 30일)
Tyler Smith
Tyler Smith 2016년 7월 8일
댓글: Tyler Smith 2016년 7월 9일
I have 4 columns of data in an array. The first three columns are year, month, day. The 4th column is a z-score. I need to compute the average of the 4th column for as long as the date increases by one day. When there is more than a 1 day difference between the dates the average needs to stop so that the average is only of consecutively increasing dates. Then a new average will pick up for the next set of consecutively increasing dates. Here is the first few rows of my data. Column 1 (years) = (1950,1950,1950,1950,1951,1951,1958,1958,1958,1959), Column 2 (month) = (11,11,11,11,3,3,2,2,2,3), Column 3 (day) = (22,23,24,25,11,12,15,16,17,2), and Column 4 (zscore) = (-.7,-.5,-1.1,-1.3,-1.6,-.8,-.5,-1.4,1,.5).

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 8일
years = [1950,1950,1950,1950,1951,1951,1958,1958,1958,1959]'
month= [11,11,11,11,3,3,2,2,2,3]'
day= [22,23,24,25,11,12,15,16,17,2]'
zscore =[-.7,-.5,-1.1,-1.3,-1.6,-.8,-.5,-1.4,1,.5]'
n=numel(years)
D=[years month day]
idx=[0 ;diff(datenum(D))]
ii=cumsum(not(idx==1))
z=cell2mat(accumarray(ii,(1:size(D,1))',[],@(x) {mean(zscore(x))*ones(numel(x),1)}))
out=[D z]

추가 답변 (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