Daywise differences in array
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
0 votes
Share a link to this question
Accepted Answer
0 votes
Share a link to this answer
11 Comments
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3279440
Hi @Poulomi,
To address your request for analyzing the rainfall data and determining whether the difference in rainfall values exceeds 30 mm within a 24-hour period, we need to modify the approach slightly. Your initial code iterates through each time point but only checks the next day's value, rather than considering all values within the previous 24 hours. This requires a more efficient method that avoids nested loops. You can utilize MATLAB’s capabilities with timetables to streamline this process. The following code implements a more efficient approach using logical indexing and vectorized operations:
% Input data as provided R = [1982 5 1 3 25; 1982 5 1 6 30; 1982 5 1 12 35; 1982 5 1 18 40; 1982 5 2 0 45; 1982 5 2 3 45; 1982 5 2 6 50; 1982 5 2 12 55; 1982 5 2 18 55; 1982 5 3 0 60; 1982 5 3 3 65; 1982 5 3 6 80; 1982 5 3 12 90; 1982 5 3 18 105; 1982 5 4 0 115; 1982 5 4 3 115; 1982 5 4 6 115; 1982 5 4 12 115; 1982 5 4 18 115; 1982 5 5 3 30];
% Create a timetable
ttR = timetable(datetime(R(:,1:3)) + hours(R(:,4)), R(:,5),
'VariableNames',
{'Rainfall'});
% Initialize variables to hold results t = []; r = [];
% Loop through each time point for i = 1:height(ttR) % Define the time window for the previous 24 hours startTime = ttR.Time(i) - caldays(1); endTime = ttR.Time(i);
% Find all rainfall values within this time window
rainInWindow = ttR.Rainfall(ttR.Time >= startTime & ttR.Time <
endTime); if ~isempty(rainInWindow)
% Calculate the difference between current rainfall and
maximum in window
dRF = max(rainInWindow) - ttR.Rainfall(i); if dRF > 30
t = [t; ttR.Time(i)];
r = [r; dRF];
end
end
end% Create result timetable
ttDRF = timetable(t, r, 'VariableNames', {'24hr_Rainfall'});
ttDRF.Properties.DimensionNames(1) = {'Begin_Date'};
% Display results disp(ttDRF);
first timetable is created from your data which allows for easier time-based indexing. For each time point, we calculate the start and end of the previous 24-hour window, using logical indexing to extract all rainfall values within that time window, computing the difference between the maximum rainfall in that window and the current time's rainfall. If the difference exceeds 30 mm, we store the timestamp and difference.
Feel free to run this code snippet in your MATLAB environment, and it should yield results based on your specified criteria for identifying significant rainfall differences over a defined time frame.
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3279605
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3279610
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3280010
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3280015
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3280075
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3280375
Share a link to this comment
https://www.mathworks.com/matlabcentral/answers/2158440-daywise-differences-in-array#comment_3281175
More Answers (0)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)