Re: for cicle inside an If condition
- To: mathgroup at smc.vnet.net
- Subject: [mg118631] Re: for cicle inside an If condition
- From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
- Date: Wed, 4 May 2011 19:49:13 -0400 (EDT)
Hi,
the error message suggests, that you give Select nothing where it really
*can* select something from. For instance
Select[43, # < 4 &]
would try to select any part from 43 which is smaller than 4. This makes
no sense and Mathematica tells you so.
Ok, start from the beginning. You have a matrix of numbers (!!). Lets
take an example: Say this is the first row of your matrix:
{4.05249, 2.0836, 3.82281, 1.68126, 4.57036, 0.697649, 7.66875,
2.77411, 1.87648}
When I look into your code it suggests that the first number here is the
idGRB, the second is z, the third is fx and so on. So we have
{idGRB_, z_, fx_, fxErr_, beta_, dbeta_, logTarest_, logTaErrrest_,
class_}
I know that class is not at ninth position, but its only a sample and
you'll have to adjust this anyway.
Lets create a matrix with 20 randomly chosen rows of length 9 like the
above one:
data = RandomReal[{0, 10}, {20, 9}]
You want know only those rows which have at second position "z" a value
smaller than 4.05 or exactly 5.2 or 8.26. *Here* you use the Select to
create a new matrix where only those rows are in, which fulfill this
condition:
data2 = Select[data,
With[{z = #[[2]]}, z <= 4.05 || z == 5.2 || z == 8.26] &]
Now write yourself a function which takes *one* row and creates the
output you want for this one row. (I simplified the calculation of the
lx!)
func[{idGRB_, z_, fx_, fxErr_, beta_, dbeta_, logTarest_,
logTaErrrest_, class_}] :=
With[{lx = 4 + z + beta*fx, Om = 1, Ol = 1}, {logTarest, Log[10, lx],
z, idGRB, Om, Ol}]
When you have this function, then you do nothing more than apply it to
every row of data2 and you have your result:
func /@ data2
I hope you noticed, that you don't need an explicit loop, you don't need
to append your current result to a global list and you don't need to
extract all the values (like idGRB) for each iteration.
Cheers
Patrick
On Wed, 2011-05-04 at 12:08 +0100, maria giovanna dainotti wrote:
> 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
> >
>
>
>