Remove specific values from array
507 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ana Gabriela Guedes
el 13 de Abr. de 2021
Comentada: Adam Danz
el 14 de Abr. de 2021
Hi!
I have a vector with a lot of numbers, for example, A = [9,1,2,5,1,2,5,1,1,5,2,3,1,2,5,1,2,5,4,1,2,5,10]; and I want to remove all the values that are different from 1,2,5,9 or 10. In this case I would want to remove 3 and 4 so A would be
A = [9,1,2,5,1,2,5,1,1,5,2,1,2,5,1,2,5,1,2,5,10];
(I want to apply this to a vector with hundreds of values so I cannot remove that separately, I probably need do do a cycle but its not working)
How can I do this easily?
0 comentarios
Respuesta aceptada
DGM
el 13 de Abr. de 2021
Editada: DGM
el 13 de Abr. de 2021
Something like this:
A = [9,1,2,5,1,2,5,1,1,5,2,3,1,2,5,1,2,5,4,1,2,5,10] % input
x = [1,2,5,9,10]; % values to keep
B = A(ismember(A,x))
4 comentarios
Adam Danz
el 14 de Abr. de 2021
ismember(A,x) returns a logical index. Use that logical index to remove (or keep) values in both A and B.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing 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!