Reputation: 829
I have a matrix in which the right-most elements are repeated YYYYMMDD dates in descending order, for example:
40 1630 1711 20140326
169 700 950 20140326
40 1630 1711 20140326
169 700 950 20140327
40 1630 1711 20140327
169 700 950 20140327
40 1630 1711 20140327
169 700 950 20140328
40 1630 1711 20140328
169 700 950 20140328
40 1630 1711 20140328
3049 700 950 20140331
40 1630 1711 20140331
3049 700 950 20140331
40 1630 1711 20140331
169 700 950 20140401
40 1630 1711 20140401
169 700 950 20140401
40 1630 1711 20140401
169 700 950 20140402
40 1630 1711 20140402
Within each date, I want to keep only the row that corresponds to the largest element in the leftmost column. So I would like to produce a new matrix:
169 700 950 20140326
169 700 950 20140327
169 700 950 20140328
3049 700 950 20140331
169 700 950 20140402
What is the best way to do this?
Ideas: use diff to find indices of when date changes, and use a for loop to cycle through intervals using max to find the correct rows. any solution without a for loop?
Upvotes: 2
Views: 86
Reputation: 14939
Try to first sort the rows based on the first column, thereafter find the first unique dates based on the last:
B = sortrows(A)
[~, ia] = unique(B(:,4))
B(ia,:)
ans =
169 700 950 20140326
169 700 950 20140327
169 700 950 20140328
3049 700 950 20140331
169 700 950 20140401
169 700 950 20140402
Upvotes: 5