|
"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
|