How to condionally keep unique rows in a table

조회 수: 17 (최근 30일)
Blue
Blue 2020년 6월 2일
답변: Akira Agata 2020년 6월 3일
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