2

I want to replace values in an interval [a,b] by 0 in a list of values.

Example:

{{.5,.2,.49,1,1},{.49,.5,.495,1}}

I want to get:

{{0,.2,0,1,1},{0,0,1,1}}

I try like :

{{.5,.2,.49,1,1},{.49,.5,.495,1}}/.{(#>=.49&&#<=.5)&->0}

But it does not work.

BetterEnglish
  • 2,026
  • 13
  • 19
  • I have marked this question as "already has an answer" -- please see the link inserted at the top of your Question. A number of methods are provided there, including Condition as expressed by Karsten below. I encourage you to familiarize yourself with other options such as the numeric methods as they can have significant performance advantages. Please let me know if you have any trouble applying methods shown there to your specific problem. – Mr.Wizard Oct 18 '14 at 05:47
  • @Mr.Wizard, thanks, you are very kind – BetterEnglish Oct 18 '14 at 05:59
  • @Mr.Wizard, I remark that Condition is slow. How can I use Clip[] in my case? – BetterEnglish Oct 18 '14 at 16:47

2 Answers2

2

You can use Condition to get it working:

{{.5, .2, .49, 1, 1}, {.49, .5, .495, 1}} /. x_ /; .49 <= x <= .5 -> 0
{{0, 0.2, 0, 1, 1}, {0, 0, 0, 1}}
Karsten7
  • 27,448
  • 5
  • 73
  • 134
1

you can also use pattern test ?

{{.5, .2, .49, 1, 1}, {.49, .5, .495, 1}} /. _?(.49 <= # <= .5 &) :> 0
(*{{0, 0.2, 0, 1, 1}, {0, 0, 0, 1}}*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78