Problem 58339. Remove runs of at least n consecutive NaNs
This problem is inspired by Dyuman Joshi's problem 58329. Given a row vector x and a natural number n, remove all runs of at least n consecutive NaNs from x, leaving all shorter runs as well as all non-NaN elements intact.
For instance, given
x = [1 NaN 2 3 NaN NaN 4 5 6 NaN NaN NaN 7 8 9 10 NaN NaN NaN NaN]
the results would be as follows:
>> removenans1n(x, 1)
ans =
1 2 3 4 5 6 7 8 9 10
>> removenans1n(x, 2)
ans =
1 NaN 2 3 4 5 6 7 8 9 10
>> removenans1n(x, 3)
ans =
1 NaN 2 3 NaN NaN 4 5 6 7 8 9 10
>> removenans1n(x, 4)
ans =
1 NaN 2 3 NaN NaN 4 5 6 NaN NaN NaN 7 8 9 10
>> removenans1n(x, 5)
ans =
1 NaN 2 3 NaN NaN 4 5 6 NaN NaN NaN 7 8 9 10 NaN NaN NaN NaN
with no changes in output for even higher n (since x only contains runs of NaNs up to length four).
To make this challenging, provide a vectorized solution: no loops, no arrayfun (in fact, no fun at all, though you're still allowed to have fun), and no recursion. Regular expressions are also outlawed (I don't think that this is a problem that is naturally solved using regular expressions, so I don't feel too bad about doing so).
Solution Stats
Solution Comments
Show commentsProblem Recent Solvers4
Suggested Problems
-
Number of 1s in the Binary Representation of a Number
444 Solvers
-
Find perfect placement of non-rotating dominoes (easier)
350 Solvers
-
Make an awesome ramp for a tiny motorcycle stuntman
653 Solvers
-
Wind outward from the center ...
84 Solvers
-
Create logical matrix with a specific row and column sums
303 Solvers
More from this Author16
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!