필터 지우기
필터 지우기

sum all monthly values for every year

조회 수: 1 (최근 30일)
Colby
Colby 2015년 2월 2일
댓글: Scott Miller 2015년 10월 8일
I have a large matrix A. In column 1, I have serial dates. They are in half month time steps. (ex ...10/01/1984, 10/15/1984, 11/01/1984,.... or as serial dates ...724916, 724930, 724947, ...) Then in the second column, are values for each date. I would like a new matrix that has the sum of the values in column b, for every year in column a. For the life of me, I can't figure out a simple way to execute this. Does anyone have a simple answer? Thanks a lot. I appreciate your time!

채택된 답변

Guillaume
Guillaume 2015년 2월 2일
Easy:
dv = datevec(A(:, 1)); %convert to datevec to easily separate years
[years, ~, subs] = unique(dv(:, 1)); %get unique years and location
yearsum = accumarray(subs, A(:, 2)); %accumarray with most default values does sums
  댓글 수: 2
Colby
Colby 2015년 2월 2일
Thank you very much Guilaume!
Scott Miller
Scott Miller 2015년 10월 8일
Thanks, Guillaume! Adding an @max function handle to the accumarray function in your code sample is a slick way to single out peak annual flows from a stream flow or runoff time series.
pkQ = accumarray(subs, tsQ(:, 2),[],@max);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by