Borrar filtros
Borrar filtros

find the last non-zero value

90 visualizaciones (últimos 30 días)
Mahsa
Mahsa el 27 de Mzo. de 2015
Editada: Image Analyst el 27 de Mzo. de 2015
For a regular vector we can use following command to extract the vector after pre-allocating.
if m = [ 1 2 3 4 5 0 0 0 0 0 0 0 0]
m(m==0)=[]
m= [1 2 3 4 5];
However my question is what to do if zero is one of the real values like the following example:
m = [ 1 2 0 3 4 0 5 0 0 0 0 0 0 0 0];
I'm looking for a command that gives me
m = [ 1 2 0 3 4 0 5]
Thank you

Respuesta aceptada

Stephen23
Stephen23 el 27 de Mzo. de 2015
Editada: Stephen23 el 27 de Mzo. de 2015
Use find to locate non-zero values in an array. The optional arguments help you too:
>> m = [ 1 2 0 3 4 0 5 0 0 0 0 0 0 0 0];
>> m(1:find(m,1,'last'))
ans =
1 2 0 3 4 0 5

Más respuestas (0)

Categorías

Más información sobre Mathematics 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!

Translated by