Filter löschen
Filter löschen

Find certain row out of many rows with the same id

2 Ansichten (letzte 30 Tage)
Triple G
Triple G am 22 Jun. 2020
Beantwortet: Walter Roberson am 23 Jun. 2020
I have a matrix whose each row consists of an id column and some other columns with other features of each object. One of the characteristics is a date. Now, some rows of the matrix have the same id and I want to keep only the one with the most recent date. For example, in the table above, the id is in the third column, so, from the second and third row I want to keep only the third, as it has the most recent date. How could I do that in a matrix with many subsets of rows with the same id?
490 673 9987 {'2015-09-10 00:00:00'} 58
491 110556 10233 {'2010-02-22 00:00:00'} 70
492 110556 10233 {'2011-02-22 00:00:00'} 65

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Jun. 2020
One approach:
sortrows() on the ID column, and the date column in descending order. unique() the ID column with 'stable' and take the second output. Use that to index the sortrows output to get just the first (newest because descending order) entry for each ID.
If necessary re-sort on the first column.
Or...
findgroups() on the ID, and splitapply() specifying a function that will receive the variable entries that are associated with a single ID. The function would datetime() and max() to get the index of the maximum and return that entry from the variables.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by