我模拟了一整年的风速数据,现在我想计算风速低于某一阈值的连续小时数。我得找出低于这个阈值的最大连续时间。因此,它应该在达到阈值时开始计数,并在值再次超过阈值时停止计数。这种情况在一年中会发生多次,所以,就像我提到的,我寻找的是最大连续小时数
Windspeed_hourly_mean = xlsread('Windspeed_hourly.xlsx','D3:D8762');
WHM=Windspeed_hourly_mean;
threshold = 3;
isBelow = (WHM < threshold); % gives us a vector of logical values
isBelow = [false, isBelow, false]; % make sure the vector starts and ends with false
transitions = diff(isBelow); % gives 1 where false->true and -1 where true->false
starts = find(transitions==1); % indexes of where each period starts
ends = find(transitions==-1); % indexes of where each period ends
durations = ends - starts;
max(durations) % largest duration
如果你有一个每小时风速值windSpeed
的1 xn向量,你的阈值是threshold
,那么:
isBelow = (windSpeed < threshold); % gives us a vector of logical values
isBelow = [false, isBelow, false]; % make sure the vector starts and ends with false
transitions = diff(isBelow); % gives 1 where false->true and -1 where true->false
starts = find(transitions==1); % indexes of where each period starts
ends = find(transitions==-1); % indexes of where each period ends
durations = ends - starts;
max(durations) % largest duration
需要将false
添加到isBelow
向量的开始和结束,以便在每个结束之前都有一个开始,并且使ends
向量与starts
向量长度相同。