How to condionally keep unique rows in a table

17 次查看(过去 30 天)
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,

采纳的回答

Akira Agata
Akira Agata 2020-6-3
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,:);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by