How to condionally keep unique rows in a table

17 visualizzazioni (ultimi 30 giorni)
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,

Risposta accettata

Akira Agata
Akira Agata il 3 Giu 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,:);

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by