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 で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!