0
% counting the number of transitions from state 0 to 1,
% rain is an array of size 545.
    count1=0;
    n=numel(rain);
    for k=1:n-1,
        if (rain(k)<=0) & (10<rain(k+1)<20),
            count1=count1+1;
        end
    end
    display(count1)
    display(n)
P0W
  • 46,614
  • 9
  • 72
  • 119

1 Answers1

2
sum(rain(2:end) > 10 & rain(2:end) < 20 & rain(1:end-1) = 0)

rain(1:end-1): Get all the rain data bar the last element rain(2:end): Get all the rain data bar the first element. The reason for this is to shift the data one element forward so that it's easy to search for a previous value of zero. (i.e. previous values are now in the same position as the values you want to check the limits for)

rain > 10 will return a logical vector with 1s where it is greater than 10 and 0s otherwise. Calling sum on this just adds up all the 1s so it proxies for counting them.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Thanks, this worked. Can u please elaborate your code a bit? Why did you use 2:end as bounds and what does sum function return? – Dipankar Choudhary Apr 08 '14 at 15:46
  • @DipankarChoudhary no problem, check my edit. This is best understood by executing each part of it separately in the command line on a subset of rain leaving off the semicolons so you can see what each part does. – Dan Apr 08 '14 at 15:53