Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1930

Re: for cicle inside an If condition

From Patrick Scheibe <pscheibe@trm.uni-leipzig.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: for cicle inside an If condition
Date 2011-04-30 09:53 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ipgm6a$8rs$1@smc.vnet.net> (permalink)

Show all headers | View raw


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
> 


Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: for cicle inside an If condition Patrick Scheibe <pscheibe@trm.uni-leipzig.de> - 2011-04-30 09:53 +0000

csiph-web