Advice on Indexing A table.
조회 수: 1 (최근 30일)
이전 댓글 표시
Good Afternoon,
I have another probably easy to answer question.
If I have a table like the one below. Is it possible to sort it so that for any number such as 38 here, I keep only the row with the earliest date and remove the rest? For this example I would only have -- 38 '07/07/2014'?
Noting too that the earliest date is not always the first entry.
Any advice or videos to watch would be helpful and very much appreciated.
Thank you!
35 '24/07/2014'
36 '08/07/2014'
38 '06/08/2014'
38 '07/07/2014'
38 '11/07/2014'
38 '11/08/2014'
38 '14/07/2014'
38 '15/07/2014'
38 '16/07/2014'
38 '28/07/2014'
46 '05/08/2014'
46 '14/08/2014'
댓글 수: 0
답변 (2개)
Azzi Abdelmalek
2015년 8월 4일
편집: Azzi Abdelmalek
2015년 8월 4일
a={35 '24/07/2014'
36 '08/07/2014'
38 '06/08/2014'
38 '07/07/2014'
38 '11/07/2014'
38 '11/08/2014'
38 '14/07/2014'
38 '15/07/2014'
38 '16/07/2014'
38 '28/07/2014'
46 '05/08/2014'
46 '14/08/2014'}
c1=cell2mat(a(:,1));
c2=datenum(a(:,2),'dd/mm/yyyy');
[ii,jj,kk]=unique(c1);
min_date=accumarray(kk,c2,[],@(x) {datestr(min(x))});
out=[num2cell(ii) min_date]
Depending on what you mean by earliest, use min or max
댓글 수: 0
Peter Perkins
2015년 8월 5일
Let's assume you have these in your workspace:
>> x = [35 36 38 38 38 38 38 38 38 38 46 46]';
>> date = {'24/07/2014' '08/07/2014' '06/08/2014' '07/07/2014' '11/07/2014' '11/08/2014' '14/07/2014' '15/07/2014' '16/07/2014' '28/07/2014' '05/08/2014' '14/08/2014'}';
Using R2014b or later, make a table, and convert the strings to datetimes (notice that it figured out that you meant day/month/year, and not month/day/year):
>> data = table(x,date);
>> data.date = datetime(data.date)
data =
x date
__ ___________
35 24-Jul-2014
36 08-Jul-2014
38 06-Aug-2014
38 07-Jul-2014
38 11-Jul-2014
38 11-Aug-2014
38 14-Jul-2014
38 15-Jul-2014
38 16-Jul-2014
38 28-Jul-2014
46 05-Aug-2014
46 14-Aug-2014
If the data are actually in a file, there are other ways to do that, but let's assume workspace variables. Now sort the table, and find the first row for each unique value of x. That's your desired result.
>> data = sortrows(data);
>> [~,ui] = unique(data.x,'stable');
>> udata = data(ui,:)
udata =
x date
__ ___________
35 24-Jul-2014
36 08-Jul-2014
38 07-Jul-2014
46 05-Aug-2014
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!