How to remove numbers from a vector?

6 visualizaciones (últimos 30 días)
rafe brooks
rafe brooks el 30 de Dic. de 2020
Comentada: rafe brooks el 5 de En. de 2021
I am using graphical data and looping round a set of data. So, how can I write an if statement to remove zeros from the start and end of the vector so that only the needed data is there, I dont want to remove all zeros though as the middle section of all the data sets has zeros which are needed?
The variable that the data sets loop through is called z_force

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Dic. de 2020
minidx = find(z_force, 1, 'first')
maxidx = find(z_force, 1, 'last')
z_force(minidx:maxidx)
  2 comentarios
Image Analyst
Image Analyst el 30 de Dic. de 2020
minidx = find(z_force, 1, 'first')
maxidx = find(z_force, 1, 'last')
z_force = z_force(minidx:maxidx) % Extract and assign back to z_force, overwriting the original.
rafe brooks
rafe brooks el 5 de En. de 2021
Thanks for the help, thats great!

Iniciar sesión para comentar.

Más respuestas (1)

William
William el 30 de Dic. de 2020
You could use something like:
while z_force(1)==0
z_force(1) = [];
end
while z_force(end)==0
z_force(end) = [];
end

Categorías

Más información sobre Loops and Conditional Statements 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