Thread Subject: summing the most recent instance of consecutive values

Subject: summing the most recent instance of consecutive values

From: Hamish

Date: 25 Nov, 2009 05:12:00

Message: 1 of 5

hi all. i have a string of values (daily rainfall) and am calculating a formula designed to capture the influence of the last 20 values.

everything hinges around the definition of an event
event=(most recent instance of) consecutive values>2 (in the last 20 values) i.e. if there are multiple events within 20 days, only the most recent one counts

i need to calculate N and P
N= number of days since event maximum
P = sum of rainfall over the event

a sample string (from least recent to yesterday)
0, 0, 2, 0, 2, 3, 4, 0, 11, 12, 0, 0, 0, 0, 2, 5, 10, 0, 0, 0, 0, 11, 16, 3, 0, 0

to calculate today's N and P, the most recent event is 11,16,3, N = 4 and P = 30.

i started by creating a 20 x n matrix ie n lots of the most recent 20 values, (ignoring the first 20 days) within this i can remove values of 2 or less. i can also use [Nmax,N]=max(20 x n matrix), with Nmax being the highest value in the last 20 days and N giving the time since. but this does not help where the highest value occurred in an event other than the most recent one. it's about here that i get stuck.

i'd appreciate any advice whatsoever! thanks in advance for your help

Hamish

Subject: summing the most recent instance of consecutive values

From: Darren Rowland

Date: 25 Nov, 2009 08:36:19

Message: 2 of 5

"Hamish " <hamishomitthis@unsw.edu.au> wrote in message <heieb0$n5j$1@fred.mathworks.com>...
> hi all. i have a string of values (daily rainfall) and am calculating a formula designed to capture the influence of the last 20 values.
>
> everything hinges around the definition of an event
> event=(most recent instance of) consecutive values>2 (in the last 20 values) i.e. if there are multiple events within 20 days, only the most recent one counts
>
> i need to calculate N and P
> N= number of days since event maximum
> P = sum of rainfall over the event
>
> a sample string (from least recent to yesterday)
> 0, 0, 2, 0, 2, 3, 4, 0, 11, 12, 0, 0, 0, 0, 2, 5, 10, 0, 0, 0, 0, 11, 16, 3, 0, 0
>
> to calculate today's N and P, the most recent event is 11,16,3, N = 4 and P = 30.
>
> i started by creating a 20 x n matrix ie n lots of the most recent 20 values, (ignoring the first 20 days) within this i can remove values of 2 or less. i can also use [Nmax,N]=max(20 x n matrix), with Nmax being the highest value in the last 20 days and N giving the time since. but this does not help where the highest value occurred in an event other than the most recent one. it's about here that i get stuck.
>
> i'd appreciate any advice whatsoever! thanks in advance for your help
>
> Hamish

Hamish,
Here is a function which computes the result for a single row.
The days since the event uses the time since the centre of the event.

function [timeSinceEvent, totalRain] = lastevent(A)
c = diff([0 A 0]>2);

j = length(c);
k = j;
while c(j)~=1 || c(k)~=-1 || (k <= j+1 && c(j)==1 && c(k)==-1)
    j = j - 1;
    if c(j)== -1
        k = j;
    end
end

k = k - 1;
totalRain = sum(A(j:k));
timeSinceEvent = length(A) + 1 - (k+j)/2;


It should not be very hard to vectorise.
Hth
Darren

Subject: summing the most recent instance of consecutive values

From: Hamish

Date: 25 Nov, 2009 23:01:18

Message: 3 of 5

thanks a jazillion Darren, i'll give it a whirl!

Subject: summing the most recent instance of consecutive values

From: the cyclist

Date: 26 Nov, 2009 04:33:19

Message: 4 of 5

"Hamish " <hamishomitthis@unsw.edu.au> wrote in message <heieb0$n5j$1@fred.mathworks.com>...
> hi all. i have a string of values (daily rainfall) and am calculating a formula designed to capture the influence of the last 20 values.
>
> everything hinges around the definition of an event
> event=(most recent instance of) consecutive values>2 (in the last 20 values) i.e. if there are multiple events within 20 days, only the most recent one counts
>
> i need to calculate N and P
> N= number of days since event maximum
> P = sum of rainfall over the event
>
> a sample string (from least recent to yesterday)
> 0, 0, 2, 0, 2, 3, 4, 0, 11, 12, 0, 0, 0, 0, 2, 5, 10, 0, 0, 0, 0, 11, 16, 3, 0, 0
>
> to calculate today's N and P, the most recent event is 11,16,3, N = 4 and P = 30.
>
> i started by creating a 20 x n matrix ie n lots of the most recent 20 values, (ignoring the first 20 days) within this i can remove values of 2 or less. i can also use [Nmax,N]=max(20 x n matrix), with Nmax being the highest value in the last 20 days and N giving the time since. but this does not help where the highest value occurred in an event other than the most recent one. it's about here that i get stuck.
>
> i'd appreciate any advice whatsoever! thanks in advance for your help
>
> Hamish

The following code should do what you want. It finds the location of the last increase (which is the last peak). Then it finds most recent zero that is before that peak, and sums all rainfall after that (which is all the rainfall in the last event).

>> rain = [0, 0, 2, 0, 2, 3, 4, 0, 11, 12, 0, 0, 0, 0, 2, 5, 10, 0, 0, 0, 0, 11, 16, 3, 0, 0];
>>
>> indexToLastIncrease = find(diff([0,rain])>0,1,'last');
>> P = length(rain) - indexToLastIncrease + 1
>>
>> indexToZeros = find(rain==0);
>> indexToLastZeroBeforeLastIncrease = max(indexToZeros(indexToZeros<indexToLastIncrease));

>> N = sum(rain(indexToLastZeroBeforeLastIncrease+1:end))

the cyclist

Subject: summing the most recent instance of consecutive values

From: Hamish

Date: 26 Nov, 2009 04:40:17

Message: 5 of 5

"the cyclist" <thecyclist@gmail.com> wrote in message <hel0ef$id8$1@fred.mathworks.com>...

thank you cyclist! it's great to have a different approach to try, and compare with the other

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
code Darren Rowland 25 Nov, 2009 03:35:56
most recent Hamish 25 Nov, 2009 00:14:05
cumulative sum Hamish 25 Nov, 2009 00:14:05
consecutive Hamish 25 Nov, 2009 00:14:04
rssFeed for this Thread

Contact us at files@mathworks.com