Borrar filtros
Borrar filtros

how to delete rows with some repeated elements?

2 visualizaciones (últimos 30 días)
Iris Li
Iris Li el 30 de Mayo de 2018
Comentada: Iris Li el 31 de Mayo de 2018
for example I need to delete one out of two rows whose numbers in column 1&2 are the same. At the same time the deleted row should have a smaller number in column 3. I know the unique command but don't know how to use in this situation. Thanks in advance. I have a=[1,2,3; 1,2,4; 5,6,7; 5,6,8];I want to get b=[1,2,4;5,6,8]

Respuesta aceptada

Stephen23
Stephen23 el 30 de Mayo de 2018
Editada: Stephen23 el 30 de Mayo de 2018
One way using sortrows and unique:
>> a = [1,2,3;1,2,4;5,6,7;5,6,8];
>> b = sortrows(a,3);
>> [~,idx] = unique(b(:,1:2),'rows','last');
>> b = b(idx,:)
b =
1 2 4
5 6 8
The sortrows call sorts rows by the third column (i.e. smaller values come before larger), then we select the unique rows (only columns 1 & 2), taking the last one each time (which because the rows are sorted will be the larger value in the third column).

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by