Find all numeric values right after the NaN values in a column vector
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Konstantinos Tsitsilonis
el 22 de Jun. de 2017
Editada: Konstantinos Tsitsilonis
el 22 de Jun. de 2017
Good evening,
I have a column vector such that
vec = [1 ; 2 ; 3 ; 4 ; Nan ; 5 ; 6 ; 7 ; 8 ; Nan ; 9 ; 10 ; 11 ; 12 ; Nan];
I would like to find extract the elements of the vector ''vec'' that are located right after the NaN, such that I would get a new vector:
new_vec=[5;9]
It is very easy to find the indices of all the NaN elements, however I don't know how to 'shift' those indices a place further to locate the values right after the NaN.
Thanks for your help in advance,
KMT
0 comentarios
Respuesta aceptada
Jan
el 22 de Jun. de 2017
Editada: Jan
el 22 de Jun. de 2017
index = isnan(vec);
result = vec([false; index(1:end-1)]);
Or in one line:
result = vec([false; isnan(vec(1:end-1))]);
This is "logical indexing" and "shifting" is simply to insert a FALSE at the beginning.
1 comentario
Konstantinos Tsitsilonis
el 22 de Jun. de 2017
Editada: Konstantinos Tsitsilonis
el 22 de Jun. de 2017
Más respuestas (1)
James Tursa
el 22 de Jun. de 2017
Editada: James Tursa
el 22 de Jun. de 2017
E.g.,
x = find(isnan(vec))+1;
x = x(x<=numel(vec)); % or x(x>numel(vec)) = [];
result = vec(x);
0 comentarios
Ver también
Categorías
Más información sobre Language Fundamentals 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!