Clear Filters
Clear Filters

How to condionally keep unique rows in a table

31 views (last 30 days)
Hi,
I have the following table T and I would like to keep only the rows that are unique based on the combination of the variables T.a, T.b, T.c and T.d and where T.e == 2 and where T.f == 3. How would I do that ? The desired output is, well, desired_output
a = {'C1', 'C1', 'C1', 'C1'}';
b = [1004, 1004, 1004, 1004]';
c = [1, 1, 1, 1]';
d = [7, 7, 7, 7]';
e = [1, 1, 2, 2]';
f = [4,4,3,4]';
T = table(a, b, c, d, e, f)
[uni_comb, rows] = unique(T(:, [1:4]), 'rows');
desired_output = T(3,:)
Thank you,

Accepted Answer

Akira Agata
Akira Agata on 3 Jun 2020
How about the following?
idx = (T.e == 2) & (T.f == 3);
T_desired = unique(T(idx,:),'rows');
Or, if your original table T has columns other than a~f, then the following should work:
idx = (T.e == 2) & (T.f == 3);
T_tmp = T(idx,:);
[~,loc] = unique(T_tmp(:,{'a','b','c','d'}),'rows');
T_desired = T_tmp(loc,:);

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!