MATLAB: Selecting rows with maximum value with consecutive 3 days occurrence

matrix arraymatrix manipulation

I have following matrix in which first, second, third column show year, month, day and value from event 1. However, some of the days are consecutive. I need to choose only those days in a year which contains maximum value in column 3 out of consecutive occurrences.
1970 1 13 0.4659 1970 1 18 29.85
1970 1 14 0.4423 1970 1 18 29.85
1971 1 21 0.5851 1971 1 26 25.63
1971 1 23 0.4701 1971 1 26 25.63
1971 3 18 0.4938 1971 3 19 15.48
1972 2 2 0.4272 1972 2 2 35.33
1972 8 8 0.3888 1972 8 7 7.521
1972 12 27 0.5758 1972 12 28 25.25
1973 12 19 0.5834 1973 12 19 25.97
1973 12 20 0.5414 1973 12 19 25.97
1973 12 23 0.5011 1973 12 19 25.97
1974 1 27 0.4437 1974 1 30 51.96
1974 2 11 0.5594 1974 2 11 45.36
1974 9 2 0.49 1974 9 5 23.04
1975 1 14 0.4255 1975 1 19 17.99
1975 1 15 0.4486 1975 1 19 17.99
1975 9 27 0.381 1975 9 25 5.825
1976 10 11 0.3337 1976 10 14 25.12
1976 12 6 0.3528 1976 11 30 25.93
1977 3 15 0.4784 1977 3 15 15.4
1977 10 8 0.43136 1977 10 6 3.325
1977 12 8 0.4412 1977 12 9 24.1
1978 2 19 0.43 1978 2 25 22.38
1978 2 25 0.4208 1978 2 25 22.38
1978 12 23 0.3763 1978 12 23 24.88
1979 1 3 0.5779 1978 12 28 20.26
1979 2 8 0.4116 1979 2 7 15.01
1979 12 15 0.411 1979 12 15 21.24
1980 3 26 0.47822 1980 3 31 22.5
1980 3 27 0.436 1980 3 31 22.5
The desired output matrix is:
1971 1 13 0.46 1970 1 18 29.85
1971 1 21 0.58 1971 1 26 25.63
1971 3 18 0.49 1971 3 19 15.48
1972 2 2 0.42 1972 2 2 35.33
1972 8 8 0.38 1972 8 7 7.521
1972 12 27 0.57 1972 12 28 25.25
1973 12 19 0.58 1973 12 19 25.97
1973 12 23 0.50 1973 12 19 25.97
1974 1 27 0.44 1974 1 30 51.96
1974 2 11 0.55 1974 2 11 45.36
The associated .mat file is also attached.

Best Answer

Finding which days go together:
daygroup = cumsum([1; days(diff(datetime(Z(:, 1:3)))) > 3]);
Then, if you don't want to define a filter function:
[~, grouprow] = splitapply(@max, Z(:, 4), daygroup);
filteredZ = splitapply(@(rows, group) rows(grouprow(group(1)), :), Z, daygroup, daygroup)
If you're happy with a separate filtering function
function rows = filterrows(rows)
[~, idx] = max(rows(:, 4));
rows = rows(idx, :);
end
then,
filteredZ = splitapply(@filterrows, Z, daygroup)