2
$\begingroup$

If I have a list like that:

list = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1}

how can I filter it in such a way that noisy input (like the 2, here) is replaced by the neighbouring values, like that:

list = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
$\endgroup$

1 Answer 1

3
$\begingroup$
CommonestFilter[list, 3]

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

or

MedianFilter[list, 3]

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

Also

SequenceReplace[list, {x_,y_,x_}/; y!=x:>Sequence[x,x,x]]

{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

$\endgroup$
2
  • $\begingroup$ Very nice ! Thanks a lot! $\endgroup$
    – james
    Aug 8, 2018 at 8:50
  • $\begingroup$ @james, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Aug 8, 2018 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.