Problem 73. Replace NaNs with the number that appears to its left in the row.
Replace NaNs with the number that appears to its left in the row.
If there are more than one consecutive NaNs, they should all be replaced by the first non-NaN value to the immediate left of the left-most NaN. If the NaN is in the first column, default to zero.
For example, if
x = [NaN 1 2 NaN NaN 17 3 -4 NaN]
then
y = [ 0 1 2 2 2 17 3 -4 -4]
Solution Stats
Problem Comments
-
1 Comment
Richard Zapor
on 29 Apr 2012
The problem does not limit the array to a single row. Add a few cases of multiple rows or define as single row.
Solution Comments
Show commentsProblem Recent Solvers2975
Suggested Problems
-
Find all elements less than 0 or greater than 10 and replace them with NaN
15528 Solvers
-
6251 Solvers
-
Remove any row in which a NaN appears
8587 Solvers
-
middleAsColumn: Return all but first and last element as a column vector
605 Solvers
-
8516 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!