Problem 1048. Detect a number and replace with two NaN's
Write code which replaces the number 1 with two NaNs.
Example
X = [ 1 2 NaN 4 1 3 7 NaN 1 4 NaN 2]
Answer Y is
[ NaN NaN 2 NaN 4 NaN NaN 3 7 NaN NaN NaN 4 NaN 2]
Solution Stats
Problem Comments
-
13 Comments
The test-suite is incorrectly programmed. Isequal does not work for nan's:
isequal(nan,nan) is never true.
Furthermore, the line "if n=1" is an incorrect statement.
J.G. is right!! please correct it!
Dear all,
You can make use of 'isnan'
Try.
It can be solved...
use 'isequaln'
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN...
Batman?
the other commenters are right. see: http://www.mathworks.com/help/matlab/ref/isequalwithequalnans.html
Still, the open "if" statement in the test suite will not pass any solutions. Consider making "n" an argument?
the problem can be solved, but the test suite has bugs like everyone else commented. please fix the open "if" and isequal on NaN's i.e. assert(isequal(y_correct, y_correct)) does not pass in the second test.
Is it fine now?
Not quite. You will still need to use "isequalwithequalnans" (or "isequaln" in newer versions) instead of "isequal".
I updated the test suite with the suggestions made here. I believe it works now.
The particular number is 1
This problem would be much better if the "particular number" was one of the arguments to the function.
Solution Comments
Show commentsProblem Recent Solvers191
Suggested Problems
-
384 Solvers
-
1223 Solvers
-
Getting the indices from a matrix
648 Solvers
-
Permute diagonal and antidiagonal
447 Solvers
-
360 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!