How to condionally keep unique rows in a table
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Blue
el 2 de Jun. de 2020
Respondida: Akira Agata
el 3 de Jun. de 2020
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,
0 comentarios
Respuesta aceptada
Akira Agata
el 3 de Jun. de 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,:);
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!