how to replace a sequence of specific length of NaNs?

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2016-11-28
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by