Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Sum hourly precip data to storm events
Date: Thu, 26 Apr 2012 16:23:12 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 32
Message-ID: <jnbspg$2dp$1@newscl01ah.mathworks.com>
References: <jn7jtt$hf8$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1335457392 2489 172.30.248.37 (26 Apr 2012 16:23:12 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 26 Apr 2012 16:23:12 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:766035

"Olivia " <owright2@gmail.com> wrote in message <jn7jtt$hf8$1@newscl01ah.mathworks.com>...
> Hi all! I have hourly precipitation data that I want to sum into individual storm events. If there are 24 hours with no rainfall (value 0), the storm ends and I would like the code to start adding the values of a new storm. The resulting output will be the precipitation depth of each storm event. Anyone have any ideas how I can do this in matlab? Thank you!
- - - - - - - - - - -
  How about a simple for-loop solution?  It's easier to code than a vectorized method.  (Moreover, I am guessing it will be faster in spite of the number of lines.)

  Let x be a vector giving the hourly precipitation.

 y = zeros(size(x));
 f = false;
 iy = 0;
 for ix = 1:length(x)
   if x(ix) ~= 0
     c = 0;
     if ~f
       f = true;
       iy = iy + 1;
     end
     y(iy) = y(iy) + x(ix);
   else
     if f
       c = c + 1;
       if c >= 24
         f = false;
       end
     end
   end
 end
 y = y(1:iy);

Then y is a vector giving total precipitation for each storm.

Roger Stafford