Re: Applying function only if the output is positive
- To: mathgroup at smc.vnet.net
- Subject: [mg116394] Re: Applying function only if the output is positive
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 13 Feb 2011 03:06:23 -0500 (EST)
- References: <ij0e59$9a8$1@smc.vnet.net>
Am 10.02.2011 11:20, schrieb ma07llg2: > Hi, > > I am trying to find a way which will only apply my function if the output is positive, this is what i have but when the numbers become negative i need them to not be included anymore. > > update1[x_List] := > x + j.n[x] + Table[RandomReal[{-.5, .5}], {k, 1, Length[x]}] > w1[k_] := NestList[update1, gg, k] > w1[2] > > {{0.261588, 0.512798, 0.709287, 0.914866, 0.779221, 0.00614278, > 0.904256, 0.95166, 0.670016, 0.901556}, {0.427219, 0.656549, > 0.825981, 1.38668, 0.667352, -0.0109928, 0.78814, 0.458776, > 0.276835, 1.40125}, {-0.00600954, 0.831096, 1.58705, 2.69087, > 1.12829, -0.0721115, 0.562469, 0.343643, 0.947323, 2.76569}} > > Is this possible? any help would be really appreciated! > Thanks > Hi! Hmmm, I don't know what j,n,gg might be but if I understand you correctly, you want to delete negative outcomes in the list returned by update1. This can be achieved by applying DeleteCases: In[1]:= update1[x_]:=DeleteCases[ x+(*j.n[x]+*)RandomReal[1,{Length[x]}]-.5, y_/;y<0] In[2]:= result=NestWhileList[update1,Table[0,{10}],#=!={}&,1,4] Out[2]= {{0,0,0,0,0,0,0,0,0,0},{0.0137397,0.211387,0.393383,0.253084,0.379958,0.194911,0.187649},{0.386306,0.131838,0.462633,0.522104,0.494118},{0.246025,0.528566,0.341487,0.769756,0.670915},{0.29273,0.620243,0.895145}} hth, Peter