Finding index to values at latest dates in tall timetable

6 views (last 30 days)
I have a tall timetable containing some repeated values (a, b, or c) in one column. I want to find the rows containing the latest time associated with a, with b, and with c and get rid of the non-latest rows in the tall timetable. Here's what I tried. Is there a better way?
%% Make some example data
nRow = 100;
letterList = {'a', 'b', 'c'};
stringList = randsample(letterList, nRow, true)'; % Column containing a, b or c
timeList = datetime(2017, 03, 1) + rand(nRow, 1)*1000;
% Make a regular table
tableReg = table(timeList, ... % Date
stringList, ... % List with values of either 'a', 'b', or 'c'
rand(nRow, 1), ... % Value
'VariableNames', {'Date', 'Letter', 'Value'});
% Make a tall table
tt = tall(tableReg);
nRow = gather(height(tt)); % Confirm there are now only 3 rows in tt after trimming
disp([num2str(nRow) ' rows originally'])
%% Get index to latest value of each letter
% Sort by time in descending order, so most recent is first
tt = sortrows(tt, 'Date', 'desc');
[~, iLatestLetter, ~] = unique(tt.Letter); % Default is first value
iLatestLetter = gather(iLatestLetter);
tt = tt(iLatestLetter, :); % Trim table to only select latest values of each letter
nRow = gather(height(tt)); % Confirm there are now only 3 rows in tt after trimming
disp([num2str(nRow) ' rows after trim'])

Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!