how to delete repeated rows in a table?

6 visualizaciones (últimos 30 días)
Jayden Yeo
Jayden Yeo el 24 de Sept. de 2021
Comentada: Jayden Yeo el 27 de Sept. de 2021
I have an array M below. There are some repeated values in column 1. I would like to write a code that deletes all the rows that have repeated values in column 1. The final answer should be [518640 5.959; 522225 8.988].
This is a sample dataset. My actual data has more than 50,000 rows, and I need to delete all the repeated data. Thanks.
M = [518460 3.322; 518460 -12.984; 518520 3.798; 518520 -10.28; 518580 7.763; 518580 16.851; 518640 5.959; 522225 8.988];

Respuesta aceptada

per isakson
per isakson el 24 de Sept. de 2021
Editada: per isakson el 27 de Sept. de 2021
Test this
%%
M = [518460 3.322; 518460 -12.984; 518520 3.798; 518520 -10.28; 518580 7.763; 518580 16.851; 518640 5.959; 522225 8.988];
%%
[~,ixu] = unique( M(:,1), 'stable' );
M(ixu,:)
ans = 5×2
1.0e+05 * 5.1846 0.0000 5.1852 0.0000 5.1858 0.0001 5.1864 0.0001 5.2222 0.0001
In response to comment
If everything fails, try a for-loop
for jj = 1 : size(M,1)
ixm = find( M(jj,1) == M(:,1) );
if numel(ixm) >= 2
M(ixm,1) = nan;
end
end
M( isnan(M(:,1)), : ) = []
M = 2×2
1.0e+05 * 5.1864 0.0001 5.2222 0.0001
  3 comentarios
per isakson
per isakson el 27 de Sept. de 2021
See response in the answer.
Jayden Yeo
Jayden Yeo el 27 de Sept. de 2021
Thanks a lot.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 27 de Sept. de 2021
format long g
M = [518460 3.322; 518460 -12.984; 518520 3.798; 518520 -10.28; 518580 7.763; 518580 16.851; 518640 5.959; 522225 8.988]
M = 8×2
1.0e+05 * 5.1846 0.0000 5.1846 -0.0001 5.1852 0.0000 5.1852 -0.0001 5.1858 0.0001 5.1858 0.0002 5.1864 0.0001 5.2222 0.0001
[~, ia] = unique(M(:,1), 'stable');
M = M(ia,:)
M = 5×2
1.0e+05 * 5.1846 0.0000 5.1852 0.0000 5.1858 0.0001 5.1864 0.0001 5.2222 0.0001

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by