Delete repeated row in matrix

I have a matrix A that has n x 3 dimension.
Assume
A = [1 1 1; 2 2 2 ; 3 3 3; 1 1 1; 4 4 4]
as you see that the first row and the 4th row are repeated. I want to delete one of them without leaving a blank on that row.
And what if row 1 and row 4 are similar
A = [1 1 1; 2 2 2 ; 3 3 3; 1.1 1.1 1.1; 4 4 4]
I also want to delete one of them or average row 1 and row 4 then remove row 1 and row 4. Then replace the average in any row so that I would have 4 x 3 matrix.
Anybody knows how to do it? Thanks

 Respuesta aceptada

Rednar
Rednar el 11 de Jul. de 2013

5 votos

I think what you are looking for is the function "unique". Try using:
B = unique(A,'rows')
That will delete repeated rows in matrix A, hence the parameter 'rows'. Regarding your second question, I don't think there's a function for that exactly but you can simply average both rows and store them in a new vector X. Then create a new matrix C using the new vector X and the rows you want from your original matrix A. If I wanted to delete for example the fourth row in your last matrix A I would do:
A = [A(1:3,:);A(5,:)]
Hope that helps.

4 comentarios

Laura
Laura el 11 de Jul. de 2013
Assume we do not know which row is repeated? then how do i do it?
Rednar
Rednar el 11 de Jul. de 2013
I believe there is no defined MATLAB function for that, you would have to make the algorithm itself. What comes to my mind in order to trick the "unique" function in your case would be to do:
A = round(A)
B = unique(A,'rows')
That way, the 4th row that was 1.1 would become 1, just like in your first example and the unique function would get the job done.
Adnan Habib
Adnan Habib el 13 de Abr. de 2020
Hi, I have a similar issue. But I do not want to change the order of rows. When I use unique the order of rows changes.
Cristina Erbeia
Cristina Erbeia el 27 de Dic. de 2021
to not change the order try use command 'stable' when applying unique command

Iniciar sesión para comentar.

Más respuestas (1)

amia djamila
amia djamila el 26 de Jun. de 2019

0 votos

Hi,
Excuse me for my comment ,i have question if you can help me please, i have a tible of (1332*21) and i have some lines witch repeat ,i want to delte it i do'nt knew how i can do i it's so large that i can't check wher or how it repeat ,please help me
thank's

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Jul. de 2013

Comentada:

el 27 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by