how to replace a sequence of specific length of NaNs?

1 visualización (últimos 30 días)
pavlos
pavlos el 27 de Nov. de 2016
Respondida: Walter Roberson el 28 de Nov. de 2016
Hello,
Please help me with the following:
Consider vector
A=[2.15,NaN,NaN,1.14,NaN,NaN,0.59,NaN,NaN,0.14,NaN,NaN,0,NaN,NaN,NaN,NaN,0.08,NaN,NaN,0.39];
How can replace all the NaN sequences that are not of length=2 (i.e. not equal with the rest) with another value, e.g. with 0;
In the above example all the sequences that need to be replaced are of length = 4.
Thanks,
Pavlos

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Nov. de 2016
replacement_value = 0;
mask = [false; isnan(A(:)); false].';
nan_start = strfind(mask, [0 1]);
nan_end = strfind(mask, [1 0]);
not_len_2_idx = find( (nan_end - nan_start) ~= 2 );
Anew = A;
for K = not_len_2_idx
Anew( nan_start(K) : nan_end(K)-1 ) = replacement_value;
end

Más respuestas (0)

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!

Translated by