Replace previous values with NaN

4 visualizaciones (últimos 30 días)
MRINAL BHAUMIK
MRINAL BHAUMIK el 21 de Mzo. de 2021
Respondida: DGM el 21 de Mzo. de 2021
I have a vector with some numbers. if any of the number comes NaN then all the number before NaN will be NaN.
ex. A=[1 2 3 4 NaN 5 6 8 6]
then I want to get, A=[NaN NaN NaN NaN NaN 5 6 8 6]

Respuesta aceptada

DGM
DGM el 21 de Mzo. de 2021
This can be done using isnan() and find():
A=[1 2 3 4 NaN 5 6 8 6 1 2 3 4 NaN 5 6 8 6];
nanidx=find(isnan(A),1,'last');
A(1:nanidx)=NaN
The result will be NaN up to the last instance of NaN in the original array:
A =
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 5 6 8 6
There might be some considerations to make if A ever becomes 2D, depending on how you want to handle things.

Más respuestas (0)

Categorías

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