Advice on Indexing A table.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Azzi Abdelmalek
le 4 Août 2015
Modifié(e) : Azzi Abdelmalek
le 4 Août 2015
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 commentaires
Peter Perkins
le 5 Août 2015
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 commentaires
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!