Selecting a rain event from data series

2 次查看(过去 30 天)
Hi everyone. I hope you can help me. I have a time series of data: rain data (h in mm) recorded every 10 min in a day (column vector of 144 data). From the data I must select an event preceded and followed by 6 hours (36 data) without rain (h=0). How do I select the data range?
  2 个评论
Dyuman Joshi
Dyuman Joshi 2023-8-29
编辑:Dyuman Joshi 2023-8-29
A scalar data value recorded every 10 min for a day would give 6*24 == 144 data points per day. (6 data point an hour * 24 hours in a day)
How many data points do you collect each hour? If 1, then how did you get 1440 elements? or did you collect data for 10 days?
"From the data I must select an event preceded and followed by 6 hours (36 data) without rain (h=0). How do I select the data range?"
Using conditional operations and logical indexing - Find Array Elements That Meet a Condition
Me
Me 2023-8-29
Sorry, the zero is a typo. In one hour I have 6 logged data, so in one day 144 data. How do I select the data?

请先登录,再进行评论。

采纳的回答

Alexander
Alexander 2023-8-31
编辑:Walter Roberson 2023-9-9
I think this should work now:
clear;
load rain2.txt; rain = rain2; % Only to reuse the code above
NoOfDry = 1;
rHmm = rain(:,2);rHmm = rHmm(:)'; % To force a row vector
tH = rain(:,1)/6; % Only to have a timeline in hours, doesn't needed here
figure(1);plot(rHmm);grid;
dryTime = zeros(1,36); % 6h dry, 36*10 minutes
yDry = strfind(rHmm, dryTime); % Now we are looking for the indices dry >= 6h
if(isempty(yDry))
fprintf('In your data set are NO period(s) with ge. 6h no rain\n');
NoOfDry = 0;
return;
end
yDryDiff = diff(yDry); % 1 means > 6h dry
iNoOffDry = find(yDryDiff > 1); % Here are the positions of dry >= 6h
NoOfDry = NoOfDry+length(iNoOffDry); % This is the number of occurences dry >= 6h
fprintf('In your data set are %i period(s) with ge. 6h no rain\n',NoOfDry);
commandwindow
  3 个评论
Alexander
Alexander 2023-8-31
I think it's a good idea to accept this answer if it fulfills your needs. ;-)

请先登录,再进行评论。

更多回答(2 个)

Alexander
Alexander 2023-8-29
编辑:Alexander 2023-8-29
You can use strfind:
y = strfind(data, [0 0 0 0 0 0])
  1 个评论
Me
Me 2023-8-30
Thanks for your quick reply! "strfind" is not good for my problem.
I am attaching a txt file to better explain the problem. In the txt file you will find the indices in the first column, the rainfall height in the second. The data that I have to select are between the index 37 and 105, these data are preceded by 6 hours without rain (at least 36 data in which h=0) and are followed by 6 hours without rain (at least 36 data in which h = 0). How can I do?

请先登录,再进行评论。


Alexander
Alexander 2023-8-30
编辑:Dyuman Joshi 2023-8-30
I have to contradict, strfind is perfect if you want to avoid loops. Have a look at my rapid programmed script:
load rain.txt
tH = rain(:,1)/6; tH = tH(:)';% I prefere hours
rHmm = rain(:,2);rHmm = rHmm(:)'; % only for convenience
tHobs = tH(36:108); % I modified the observation time: 06:00 - 18:00
rHmmObs = rHmm(36:108);
figure(1);plot(tH,rHmm);grid;
dryTime = zeros(1,36); % 6h dry
figure(2);plot(tHobs,rHmmObs);grid;
yDry = strfind(rHmmObs, dryTime)
yDry = []
% Result: yDry = [], which means that no 6h w/o rain between 6-18:00
% Let's reduce the dry time to one hour:
dryTime = zeros(1,6);
yDry = strfind(rHmmObs, dryTime);
disp(yDry)
15 16 17 18 19 20 21 22 23 24 25 26 51 60 61 62 63 64
% There are a lot of consecutive numbers in, which means that there are dry
% periods which are longer than one hour. Let's test these:
yDryDiff = diff(yDry);
disp(yDryDiff)
1 1 1 1 1 1 1 1 1 1 1 25 9 1 1 1 1
% This means, you have one period of (6+11)*10 minutes w/o rain
% you have 2 periods with exact one hour w/o rain
% you have one period of (6+4)*10 minutes w/o rain
% To check item 1:
dryTime = zeros(1,17);
yDry = strfind(rHmmObs, dryTime);
disp(yDry)
15
  7 个评论
Alexander
Alexander 2023-8-30
I think I don't understand your problem. Are you looking for exact 6h dry (=0)? Or >= 6h dry? Just some code:
clear;
load rain2.txt; rain = rain2; % Only to reuse the code above
Error using load
Unable to find file or directory 'rain2.txt'.
rHmm = rain(:,2);rHmm = rHmm(:)'; % To force a row vector
tH = rain(:,1)/6; % Only to have a timeline in hours, doesn't needed here
figure(1);plot(rHmm);grid;
dryTime = zeros(1,36); % 6h dry, 36*10 minutes
yDry = strfind(rHmm, dryTime); % Now we are looking for the indices dry >= 6h
yDryDiff = diff(yDry); % 1 means > 6h dry
iNoOffDry = find(yDryDiff > 1); % Here are the positions of dry >= 6h
NoOffDry = length(iNoOffDry); % This is the number of occurences dry >= 6h
fprintf('In your data set are %i period(s) with ge. 6h no rain\n',NoOffDry)
commandwindow
If you want to get the exact time or index you have just work off the indices backward.
Just a very small piece of advice: If you really want to use the large advantages of Matlab, you should get a little bit more familiar with it. ;-)
Alexander
Alexander 2023-8-30
There is an exception I've forgotten: the first matching pattern. But I'm currently not at my PC. Hence, I will update my code tomorrow. Sorry.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by