フィルターのクリア

Delete all rows except last one with that index

4 ビュー (過去 30 日間)
R2
R2 2017 年 6 月 29 日
コメント済み: Star Strider 2017 年 6 月 30 日
I have two columns of data where I use the first as the ID and I want to delete all the rows with the same ID except for the last one with that ID. I cannot use unique as the corresponding value in the second column will always be different.
This is an over simplified example but should help explain what I mean:
2 12
4 6
5 24
5 39
5 15
5 18
11 18
14 5
14 6
23 5
Ideally I would hope to end up with the following as I always need to keep the last row with that ID:
2 12
4 6
5 18
11 18
14 6
23 5
Any help would be greatly appreciated.

採用された回答

Star Strider
Star Strider 2017 年 6 月 29 日
Use the second output from the unique function on the flipud of your matrix:
M = [2 12
4 6
5 24
5 39
5 15
5 18
11 18
14 5
14 6
23 5];
Mf = flipud(M);
[~,ix] = unique(Mf(:,1));
Result = Mf(ix,:)
Result =
2 12
4 6
5 18
11 18
14 6
23 5
The second output of unique is the first instance of each unique element, so since your data are ordered, using flipud first returns the last element.
  2 件のコメント
R2
R2 2017 年 6 月 30 日
That works perfectly thank you very much!
Star Strider
Star Strider 2017 年 6 月 30 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by