RE: Min[], Max[]
- To: mathgroup at smc.vnet.net
- Subject: [mg48326] RE: [mg48320] Min[], Max[]
- From: David.Annetts at csiro.au
- Date: Tue, 25 May 2004 07:16:41 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi Frank, > Dear newsgroup members, > > can anyone explain me what´s the sense in the definition of > > Min[{}]= Infinity and Max[{}]= -Infinity Such definitions are often made before entering a loop (in say F90). They are designed to ensure that min & max are always one of your data points. This is important if you want to normalise eg. data = data / max. since if max is not an element of your data, you will divide by 0. This is not usually what we want to do. Consider the following. lst = Table[Random[Real, {-5, 5}], {10}] mina = maxa = 0; (* this is one effect of not initialising ...*) minb = Infinity; maxb = -minb; Do[ If[lst[[ji]] < mina, mina = lst[[ji]]]; If[lst[[ji]] < minb, minb = lst[[ji]]]; Print["mina = ", mina, "; minb = ", minb, "; lst[[ji]] = ", lst[[ji]]], {ji, 1, Length@lst} ]; Output will vary from run to run (because of Random[]), but for one run, I get lst = {2.16401, 1.6797, 3.51037, -3.17384, -1.77294, 1.86815, 2.6709, 4.97422, \ -4.76049, -1.43248}. This has outputs Ji minA MinB 1 0 lst[[1]] 2 0 lst[[2]] 3 0 lst[[2]] 4 lst[[4]] lst[[4]] 5 ..... Without the "minb" definition, you need to make an additional test that min != 0 (try it!), and this is not nearly as clear. The Why do you want to initialise these (or any variable) in the first place? So you know what you are dealing with. Not initialising gives the compiler carte blanch to use what it likes, and this may not be what you need. Regards, Dave. ========================================== Dr. David Annetts EM Modelling Analyst CSIRO DEM Tel: +612 9490 5416 North Ryde Fax: +612 9490 5467 Australia David.Annetts at csiro.au ===========================================