Re: for cicle inside an If condition
- To: mathgroup at smc.vnet.net
- Subject: [mg118629] Re: for cicle inside an If condition
- From: maria giovanna dainotti <mariagiovannadainotti at yahoo.it>
- Date: Wed, 4 May 2011 19:48:52 -0400 (EDT)
Hi Patrick, Thanks a lot for your replay. I will try to explain better my problem. DataGRB is a matrix, so z is a row vector The Select command right of the /@ just extracts all values smaller than 4. Then you "map" (which is the operator /@) the function (#^2&) over these values. I included it in the code, but it gives the same error message: Nonatomic expression expected at position 1 in Select z= "1.29" My aim is to take in the loop every single values of z<4.05, but once per time not alltogether. For example here below I put manually the first value of z=0.84, in order to have (z=0.84 ||z=4.05||z=5.2||z=8.26) so the aim is to choose from the following list of z value that are stored in DataGRB[[i,2]] every value that is smaller than u, but picking only once per loop. At the end I should have many quadruplets as the number of Length[DataGRB]-3 where 3 are these three values of z ||z=4.05||z=5.2||z=8.26. For example the second quadruplet could be (z=1.29 ||z=4.05||z= 5.2||z=8.26) the third one : (z=1.95 ||z=4.05||z=5.2||z=8.26) If it is not clear, could I send you the notebook? I will be very grateful if you could help me. This is the list of z and below the loop. Thanks a lot Cheers Maria z= 1.29 z= 1.95 z= 1.44 z= 3.24 z= 2.9 z= 0.65 z= 2.82 z= 0.26 z= 3.97 z= 1.38 z= 1.71 z= 0.42 z= 2.61 z= 0.83 z= 3.35 z= 2.2 z= 0.94 z= 2.35 z= 0.08 z= 0.55 z= 2.03 z= 3.53 z= 2.3 z= 0.78 z= 3.91 z= 1.49 z= 1.51 z= 0.44 z= 3.21 z= 2.68 z= 3.8 z= 3.08 z= 0.13 z= 3.43 z= 2.71 z= 0.54 z= 0.84 z= 0.7 z= 3.69 z= 2.43 z= 0.94 z= 0.44 z= 1.26 z= 1.31 z= 2.35 z= 1.17 z= 1.49 z= 0.97 z= 2.31 z= 0.82 z= 1.16 z= 0.92 z= 3.63 z= 0.46 z= 2.45 z= 0.22 z= 2.17 z= 2.15 z= 2.69 z= 0.38 z= 2.42 z= 1.51 z= 0.77 z= 1.55 z= 2.69 z= 3.35 z= 1.97 DataGoodclosed={}; > Do[ > logTarest=DataGRB[[i,7]]; > logTaErrrest=DataGRB[[i,8]]; > Fx=DataGRB[[i,3]]; > FxErr=DataGRB[[i,4]]; > idGRB=DataGRB[[i,1]]; > z=DataGRB[[i,2]]; > class=DataGRB[[i,12]]; > beta=DataGRB[[i,5]]; > dbeta=DataGRB[[i,6]]; > If[class=33 && (z=0.84 ||z=4.05||z=5.2||z=8.26), > > Lx=4*p*dLclosed[z,Om,Ol,h]2*(1+z)-(1+beta)*Fx; > logLx=Log[10,Lx]; > AppendTo[DataGoodclosed,{logTarest,logLx,z,idGRB,Om,Ol}]], > {i,1,Length[DataGRB]}]; ________________________________ Da: Patrick Scheibe <pscheibe at trm.uni-leipzig.de> A: maria giovanna dainotti <mariagiovannadainotti at yahoo.it> Cc: mathgroup at smc.vnet.net Inviato: Sab 30 aprile 2011, 04:52:43 Oggetto: Re: [mg118463] for cicle inside an If condition Hi, in the middle of your Do loop, you say z = DataGRB[[i, 2]]; after this line z has the value of DataGRB[[i,2]]. In your If clause you go on with a condition z = DataGRB[[i, 2]] < 4 What's that supposed to do? Remember z has already the value DataGRB[[i,2]]. So if DataGRB[[i, 2]] has for instance the value 5, then you leave 5 = 5 < 4 for Mathematica to eveluate. Is that really what you want? Furthermore, it is really hard to give you some helpful advise, because it's not really clear what DataGRB is. Is it a matrix, a higher tensor, ...? But about Select: Say you have a list of numbers and want to go through all numbers that are smaller 4 and square them and give the result back. Then this is really easy: data = {11, 2, 1, 8, 11, 13, 8, 13, 1, 0, 7, 20, 17, 2, 3, 4, 7, 4}; (#^2 &) /@ Select[data, # < 4 &] The Select command right of the /@ just extracts all values smaller than 4. Then you "map" (which is the operator /@) the function (#^2&) over these values. End. Try to include it in your code. And try to get rid of these useless loops. Nest, NestWhile, NestList, Fold, Map, MapThread, MapIndexed, ... are much better choices. Cheers Patrick On Fri, 2011-04-29 at 07:30 -0400, maria giovanna dainotti wrote: > Dear Mathematica Group, > I am trying to figure out the following Problem: > In the loop below I have to choose only 4 values of z the first one should vary >from the table DataGRB[[i,2]] while the other ones are fixed, namely > > ||z=4.05||z=5.2||z=8.26 . > The problem is that > Writing in this way as below I will have all the value of DataGRB[[i,2]]< 4 > instead I would like to pick only one value per time in order that I should >have the computation for the Lx for all the possible different four values of z, > > namely each z[[i]]<4.05 and the three mentioned value of z. > > > DataGoodclosed={}; > Do[ > logTarest=DataGRB[[i,7]]; > logTaErrrest=DataGRB[[i,8]]; > Fx=DataGRB[[i,3]]; > FxErr=DataGRB[[i,4]]; > idGRB=DataGRB[[i,1]]; > z=DataGRB[[i,2]]; > class=DataGRB[[i,12]]; > beta=DataGRB[[i,5]]; > dbeta=DataGRB[[i,6]]; > If[class==33 && (z==DataGRB[[i,2]]<4 ||z==4.05||z==5.2||z==8.26, > > Lx=4*p*dLclosed[z,Om,Ol,h]2*(1+z)-(1+beta)*Fx; > logLx=Log[10,Lx]; > AppendTo[DataGoodclosed,{logTarest,logLx,z,idGRB,Om,Ol}]], > {i,1,Length[DataGRB]}]; > > I thought about a for loop or a select, but when I introduce in the loop > for example this one: > For[i=1,i<=78,i++, > {Select[z[[i]],#<4&]}] > > It gives the following error message:Select::normal: > > Nonatomic expression expected at position 1 in Select z= "1.29" > > I would be very grateful if you could help me. > > Thanks a lot in advance, > > Cheers > > Maria