Hi everyone, I want to know if there is a function which can get me a table with the first row that appears in grouped data. For example, I have this table:
key Date Height Weight
___ ____ ______ ______
4 56 34 12
21 45 123 23
21 40 67 133
45 49 64 119
45 45 12 12
The column date is already grouped in descending order.
I want to get a table with unique values for key but with the most recent date like this.
key Date Height Weight
___ ____ ______ ______
4 56 34 12
21 45 123 23
45 49 64 119
Please help

 채택된 답변

dpb
dpb 2019년 3월 28일
편집: dpb 2019년 3월 28일

2 개 추천

[~,ig]=findgroups(D.key);
[~,ig]=ismember(ig,D.key);
results in:
>> D(ig,:)
ans =
3×4 table
key Date Height Weight
___ ____ ______ ______
4 56 34 12
21 45 123 23
45 49 64 119
>>

댓글 수: 3

It works perfectly, Thanks!
How to get the last row per group in a similar situation ?
dpb
dpb 2020년 12월 14일
You could either sort by the column of interest descending or use find() with the reverse search instead of ismember with a loop construct.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

질문:

2019년 3월 28일

댓글:

dpb
2020년 12월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by