Re: List Manipulation: Conditionally changing y value when x value meets some criteria
- To: mathgroup at smc.vnet.net
- Subject: [mg109016] Re: List Manipulation: Conditionally changing y value when x value meets some criteria
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 10 Apr 2010 06:54:37 -0400 (EDT)
On 4/9/10 at 3:31 AM, cjkphd at wi.rr.com (Charles Koehler) wrote:
>I joined the data using:
>pts=MapThread[{##1}&,{x,y}]
While this will clearly work and there is nothing wrong with it,
it seems to me more direct and clearer to do
Transpose@{x,y} or even
MapThread[List, {x,y}]
>I assumed that by testing the x value using #[[1]], I could change
>the value of y using #[[2]] as shown below. If no manipulation was
>required based on the value of x, I would keep that point unchanged,
>but I'm definitely missing something.
>filtered=If[a<#[[1]]&<b,#[[2]]&=0],{#[[1]],#[[2]]}&,pts]
>a and b are the range of x values
One way to accomplish what you want would be to use pattern
matching as follows:
In[28]:= x = RandomReal[1, 10];
y = RandomReal[2, 10];
Sort[Transpose[{x, y}]] /. {a_?(.2 < # < .8 &), b_} :> {a, 0}
Out[30]= {{0.0761041, 1.91884}, {0.129938, 0.115834}, {0.304408,
0}, {0.439366, 0}, {0.505744, 0}, {0.58412, 0}, {0.616887,
0}, {0.672258, 0}, {0.808959, 0.600491}, {0.827083, 1.27203}}
Here, I sorted the x,y pairs simply to make it easy to see the
output has 0 for y whenever x is between the two specified values.