Antoine Fa
Antoine Fa

Reputation: 61

Matlab.- Erase duplicates from a cell array where each element is a timetable

I have a cell array of size 360*1 where each element is composed by a 330*3 timetable. The names for each column on every timetable are 'hour','volume' and 'price'. For each timetable, I want to erase the volume observations that are duplicated (of course, I also want to erase its corresponding price and hour). How can I do it? Unfortunately, the function 'unique' is just good when I have a vector, but not for a cell array.

Thanks in advance!

Here I provide a sample code of one timetable,

Date = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13';'2015-12-18 12:04:13';'2015-12-18 12:05:13'});
Hour = [1;1;1;1;1];
Volume = [152;152;300;400;500];
Price = [13.4;6.5;7.3;10;11];
TT = timetable(Date,Hour,Volume,Price)

The objective would be to get rid of the two 152 volume observations, and this for all the timetables contained on the cell array.

Upvotes: 1

Views: 146

Answers (1)

Max
Max

Reputation: 4045

This is pretty much just a question on how to delete elements from a table. Here is your MVE:

dts = [datetime('yesterday')
        datetime('today')
        datetime('now')
        datetime('tomorrow')];

T = timetable(dts,rand(length(dts),1),rand(length(dts),1),'VariableNames',{'price','volume'});
T.volume(4) = T.volume(2);

Note that the 4th entry of volume is the same as the second entry. Further I have assumed that volume is a vector (sounded reasonable)...

% find unique entries of the vector T.volume
[~, idx] = unique(T.volume);
% delete other rows / better: keep unique rows of the table
T = T(idx,:);

If you now cope with a cell of many tables, just loop over it. Assuming your 360x1 cell is called C:

for i = 1:length(C)
    % get table from cell
    T = C{i};
    % do the stuff above
    %...
    % assign cropped table back to the cell
    C{i} = T;
end

Upvotes: 1

Related Questions