필터 지우기
필터 지우기

calculate the accumulation of a variable over time within a loop?

조회 수: 7 (최근 30일)
Michael
Michael 2014년 9월 17일
편집: dpb 2014년 9월 17일
Hello,
I have evaporation as daily values (m). I have calculated the weighted fieldmean, which gives me a vector with length = no. of days. However, I would like to display these values as monthly sums.
How can I do this in a loop?
For example:
newsum(1) = nansum(data(1:30)) and so forth...
  댓글 수: 2
dpb
dpb 2014년 9월 17일
Do you have a corresponding date/day of month vector or does the series simply begin at a known date? And, of course, year to know if leap year or not...
Michael
Michael 2014년 9월 17일
it starts in 1993 1st January and ends 2010 31st December, i'm not sure how to do it other than every 30 days..

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

채택된 답변

Sean de Wolski
Sean de Wolski 2014년 9월 17일
Once you get the month data, the code will be almost identical to this answer:
Instead of @std, you'll want to pass in @sum
  댓글 수: 6
Michael
Michael 2014년 9월 17일
I have read the documents but I can't seem to find a way of increasing the interval to 3 or 6 months, is there a way to do this?
Michael
Michael 2014년 9월 17일
I have realised it makes more sense to calculate 3-month sums..I know how to do this the long way but wondered if I can modify this function

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

추가 답변 (1개)

dpb
dpb 2014년 9월 17일
편집: dpb 2014년 9월 17일
... to calculate 3-month sums...
There it's simpler to use another Matlab "trick" of recognizing and utilizing internal storage order and Matlab's handling of arrays. Generate the vector of monthly sums as above then to group by threes reshape and sum over the reshaped array and recast back...
mnthsum=accumarray(... % the monthly sums as above
threemnth=sum(reshape(mnthsum,3,[])).'; % sum over columns of 3 row/each
The above relies on the linear storage order and that Matlab orders arrays/matrices in column-major order and the default action of functions such as sum is to operate over columns. This "trick" is useful for any such operation over a given evenly-divisible subset of any array.
ADDENDUM
If it's decided that the monthly sums aren't needed any longer, the above can be done in place, of course...
mnthsum=sum(reshape(mnthsum,3,[])).';

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by