I want to count the number of elements which are preceded by 0 but are greater than 10 and less than 20

StackOverflow https://stackoverflow.com/questions/22941428

  •  29-06-2023
  •  | 
  •  

質問

% 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)
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top