timetable

Methods

Method Reference: timetable.unique

timetable: ttB = unique (ttA)
timetable: ttB = unique (ttA, setOrder)
timetable: [ttB, ia, ic] = unique (…)

Unique rows of a timetable.

ttB = unique (ttA) returns the distinct rows of ttA, ordered by row time. A row is its time together with its values, so two rows count as one only when they agree on both. Rows sharing a time but differing in a variable are all kept, as are rows sharing their values at different times; the row times are a column of the comparison and not a key that overrides it.

ttB = unique (ttA, setOrder) chooses the order of the result. 'sorted' is the default and 'stable' keeps the rows in the order they were met. 'first' and 'last' say which of a set of equal rows is the one reported in ia. 'rows' is accepted and changes nothing, rows being the only thing a timetable compares.

[ttB, ia, ic] = unique (…) also returns index vectors, such that ttB is ttA(ia,:) and ttA is ttB(ic,:).

A timetable with no variables is compared on its row times alone, so repeated times reduce to one. The time step is read afresh from the row times that survive.

Variables of cell, other than a cell array of character vectors, and of struct have no order to compare and are refused.

See also: sortrows, timetable

Source Code: timetable

For a timetable a row is its time together with its values, so two rows count as one only when they agree on both. Here the two readings at 01:00 differ, so both are kept, while the pair at 04:00 is identical and collapses to one.

 t0 = datetime (2024, 1, 1);
 Reading = [12; 20; 25; 44; 44];
 TT = timetable (Reading, 'RowTimes', t0 + hours ([0 1 1 4 4])')
TT =
  5x1 timetable

            Time            Reading    
    ____________________    _______    

    01-Jan-2024 00:00:00         12    
    01-Jan-2024 01:00:00         20    
    01-Jan-2024 01:00:00         25    
    01-Jan-2024 04:00:00         44    
    01-Jan-2024 04:00:00         44
 unique (TT)
ans =
  4x1 timetable

            Time            Reading    
    ____________________    _______    

    01-Jan-2024 00:00:00         12    
    01-Jan-2024 01:00:00         20    
    01-Jan-2024 01:00:00         25    
    01-Jan-2024 04:00:00         44

The row times are a column of the comparison, not a key that overrides it, so equal values at different times are all kept too.

 t0 = datetime (2024, 1, 1);
 TT = timetable ([7; 7; 7], 'RowTimes', t0 + hours (0:2)', ...
                 'VariableNames', {'Reading'});
 height (unique (TT))
ans = 3

Strip the variables and only the times are left to compare, so repeated times do reduce to one.

 D = timetable ([1; 2; 3], 'RowTimes', t0 + hours ([0 1 1])', ...
                'VariableNames', {'Reading'});
 unique (D(:, []))
ans =
  2x0 empty timetable

            Time            
    ____________________    

    01-Jan-2024 00:00:00    
    01-Jan-2024 01:00:00

The result is ordered by row time unless 'stable' asks for the order the rows were met in, and the index vectors map each way between the two timetables.

 t0 = datetime (2024, 1, 1);
 TT = timetable ([30; 10; 40; 10], 'RowTimes', t0 + hours ([2 0 3 0])', ...
                 'VariableNames', {'Depth'});
 [U, ia, ic] = unique (TT);
 U
U =
  3x1 timetable

            Time            Depth    
    ____________________    _____    

    01-Jan-2024 00:00:00       10    
    01-Jan-2024 02:00:00       30    
    01-Jan-2024 03:00:00       40
 ia
ia =

   2
   1
   3
 ic
ic =

   2
   1
   3
   1

'stable' reports them in the order they arrived instead.

 unique (TT, 'stable')
ans =
  3x1 timetable

            Time            Depth    
    ____________________    _____    

    01-Jan-2024 02:00:00       30    
    01-Jan-2024 00:00:00       10    
    01-Jan-2024 03:00:00       40