Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
Tomson writes: > Hi, I need a null array without any elements in it. Then elements will be > added to it. But how to define a null array? Matlab, I think. :-) Cheers, David -- David W. Fanning, Ph.D. Fanning Software Consulting, Inc. Phone: 970-221-0438, E-mail: david@dfanning.com Coyote's Guide to IDL Programming: http://www.dfanning.com/ Toll-Free IDL Book Orders: 1-888-461-0155
Tomson wrote: > Hi, I need a null array without any elements in it. Then elements will be > added to it. But how to define a null array? > > Best > Tomsom What is this? I don't use matlab. Would you like to do something like this. if n_elements(array) eq 0 then array=scalar else array=[array,scalar] Or in a loop for i=0,10 do begin .. if i eq 0 then array=scalar else array=[array,scalar] endfor Reimar -- Forschungszentrum Juelich email: R.Bauer@fz-juelich.de http://www.fz-juelich.de/icg/icg-i/ ============================================================ ====== a IDL library at ForschungsZentrum Juelich http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html
Reimar Bauer wrote: > Tomson wrote: > > >> Hi, I need a null array without any elements in it. Then elements will be >> added to it. But how to define a null array? >> >> Best >> Tomsom > > > What is this? I don't use matlab. > > Would you like to do something like this. > > if n_elements(array) eq 0 then array=scalar else array=[array,scalar] > > Or in a loop > > for i=0,10 do begin > .. > > if i eq 0 then array=scalar else array=[array,scalar] > endfor I'm a bit worried about showing my ignorance here - but rather than have an 'if' running on every iteration, could you not do: a=[0.5] for i=0,n do begin .. endfor a=a[1:*] cutting out n if evaluations for the sake of 1 array crop? I'll go back to my corner! Ben PS: Also, would it be possible to use the function temporary? a=temporary(a[1:*]) - would that be faster? I'm at home and not with my IDL so can't test it yet!
Ben Panter wrote: > a=[0.5] > > for i=0,n do begin Of course, I meant to write: ... a=[a,new_array] > endfor > > a=a[1:*] Cheers, Ben -- Ben Panter, Edinburgh My name (no spaces)@bigfoot which is a com.
Ben Panter wrote: > > Reimar Bauer wrote: >> Tomson wrote: >> >> >>> Hi, I need a null array without any elements in it. Then elements will be >>> added to it. But how to define a null array? >>> >>> Best >>> Tomsom >> >> >> What is this? I don't use matlab. >> >> Would you like to do something like this. >> >> if n_elements(array) eq 0 then array=scalar else array=[array,scalar] >> >> Or in a loop >> >> for i=0,10 do begin >> .. >> >> if i eq 0 then array=scalar else array=[array,scalar] >> endfor > > I'm a bit worried about showing my ignorance here - but rather than have > an 'if' running on every iteration, could you not do: > > a=[0.5] > > for i=0,n do begin > .. > endfor > > a=a[1:*] > > cutting out n if evaluations for the sake of 1 array crop? Yep - I do that often. Not just in IDL either. BTW, getting back to the original question, isn't every variable that hasn't yet been defined in an IDL procedure (an infinite number to be sure) a null array? :o\ paulv -- Paul van Delst CIMSS @ NOAA/NCEP/EMC
Ben Panter wrote: > > > Reimar Bauer wrote: > >> Tomson wrote: >> >> >>> Hi, I need a null array without any elements in it. Then elements >>> will be >>> added to it. But how to define a null array? >>> >>> Best >>> Tomsom >> >> >> >> What is this? I don't use matlab. >> >> Would you like to do something like this. >> >> if n_elements(array) eq 0 then array=scalar else array=[array,scalar] >> >> Or in a loop >> >> for i=0,10 do begin >> .. >> >> if i eq 0 then array=scalar else array=[array,scalar] >> endfor > > > I'm a bit worried about showing my ignorance here - but rather than have > an 'if' running on every iteration, could you not do: > > a=[0.5] > > for i=0,n do begin > .. > endfor > > a=a[1:*] > > cutting out n if evaluations for the sake of 1 array crop? > > I'll go back to my corner! > > Ben > > PS: Also, would it be possible to use the function temporary? > a=temporary(a[1:*]) - would that be faster? I'm at home and not with my > IDL so can't test it yet! > You are right, Reimar -- Reimar Bauer Institut fuer Stratosphaerische Chemie (ICG-I) Forschungszentrum Juelich email: R.Bauer@fz-juelich.de ------------------------------------------------------------ ------- a IDL library at ForschungsZentrum Juelich http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html ============================================================ =======
On Tue, 23 Sep 2003 20:21:44 -0700, Reimar Bauer wrote: > Tomson wrote: > >> Hi, I need a null array without any elements in it. Then elements will >> be added to it. But how to define a null array? >> >> Best >> Tomsom > > What is this? I don't use matlab. > > Would you like to do something like this. > > if n_elements(array) eq 0 then array=scalar else array=[array,scalar] > > Or in a loop > > for i=0,10 do begin > .. > > if i eq 0 then array=scalar else array=[array,scalar] endfor > > Reimar But this is also slow, since you reallocate the array on each iteration (which starts to hurt when the array gets large). Far better is to pre-allocate the array to roughly the correct size, and grow it as necessary. Search this newsgroup for "Is there a standard 'null' array" for a thread on this very topic. JD
Thanks everybody. I think this method if n_elements(array) eq 0 then array=scalar else array=[array,scalar] is quit good if I donot define array. But What I want to do is to initialize several arrays which are in common block and their length will be changed for different call and the lengths of them may be zero. If the lengthes of them are zero, the process will be changed. If there isnot any initialization, the array return to the main program may be the former ones. Best Tomson "Reimar Bauer" <R.Bauer@fz-juelich.de> ???? news:bkr2ls$cjvi$1@zam602.zam.kfa-juelich.de... > Tomson wrote: > >> Hi, I need a null array without any elements in it. Then elements will be >> added to it. But how to define a null array? >> >> Best >> Tomsom > > What is this? I don't use matlab. > > Would you like to do something like this. > > if n_elements(array) eq 0 then array=scalar else array=[array,scalar] > > Or in a loop > > for i=0,10 do begin > .. > > if i eq 0 then array=scalar else array=[array,scalar] > endfor > > Reimar > > > -- > Forschungszentrum Juelich > email: R.Bauer@fz-juelich.de > http://www.fz-juelich.de/icg/icg-i/ > ============================================================ ====== > a IDL library at ForschungsZentrum Juelich > http://www.fz-juelich.de/icg/icg-i/idl_icglib/idl_lib_intro. html >
Tomson writes: > But What I want to do is to initialize several arrays which are in common > block and their length will be changed for different call and the lengths > of them may be zero. If the lengthes of them are zero, the process will be > changed. If there isnot any initialization, the array return to the main > program may be the former ones. If I understand you correctly, you may find a pointer useful in this situation. A pointer could point to a "null array", in the sense that it would point to an "undefined variable": ptr = Ptr_New(/Allocate_Heap) IF N_Elements(*ptr) EQ 0 THEN Print, 'Pointer undefined' ELSE $ *ptr = [initValue] Cheers, David -- David W. Fanning, Ph.D. Fanning Software Consulting, Inc. Phone: 970-221-0438, E-mail: david@dfanning.com Coyote's Guide to IDL Programming: http://www.dfanning.com/ Toll-Free IDL Book Orders: 1-888-461-0155
JD Smith writes: > But this is also slow, since you reallocate the array on each iteration > (which starts to hurt when the array gets large). Far better is to > pre-allocate the array to roughly the correct size, and grow it as > necessary. Search this newsgroup for "Is there a standard 'null' array" > for a thread on this very topic. Oh, oh. I've started to write this up twice now. Good thing I'm consistent in my naming convention or I would be doing every project two or three times. Does this remind you of anything? :-( Anyway, you can find a discussion about "null arrays" here, taken from J.D.'s excellent post on the topic. http://www.dfanning.com/code_tips/nullarray.html Cheers, David -- David W. Fanning, Ph.D. Fanning Software Consulting, Inc. Phone: 970-221-0438, E-mail: david@dfanning.com Coyote's Guide to IDL Programming: http://www.dfanning.com/ Toll-Free IDL Book Orders: 1-888-461-0155
Well, David, always pluggin' the pointers, eh? Using pointers simply because of their ability to point to undefined variables seems a bit overkill to me, though. A variable in itself is just as good at representing an undefined variable... as in: if n_elements(variable) eq 0 then print,"Variable undefined" $ else variable = [initValue] And the way to "undefine" a variable is the good old dummy = temporary(variable) David Fanning <david@dfanning.com> writes: > Tomson writes: > >> But What I want to do is to initialize several arrays which are in common >> block and their length will be changed for different call and the lengths >> of them may be zero. If the lengthes of them are zero, the process will be >> changed. If there isnot any initialization, the array return to the main >> program may be the former ones. > > If I understand you correctly, you may find a pointer > useful in this situation. A pointer could point to > a "null array", in the sense that it would point to > an "undefined variable": > > ptr = Ptr_New(/Allocate_Heap) > IF N_Elements(*ptr) EQ 0 THEN Print, 'Pointer undefined' ELSE $ > *ptr = [initValue] > > Cheers, > > David > > -- > David W. Fanning, Ph.D. > Fanning Software Consulting, Inc. > Phone: 970-221-0438, E-mail: david@dfanning.com > Coyote's Guide to IDL Programming: http://www.dfanning.com/ > Toll-Free IDL Book Orders: 1-888-461-0155 -- ------------------------------------------------------------ -------------- Stein Vidar Hagfors Haugan ESA SOHO SOC/European Space Agency Science Operations Coordinator for SOHO NASA Goddard Space Flight Center, Tel.: 1-301-286-9028 Mail Code 682.3, Bld. 26, Room G-1, Cell: 1-240-354-6066 Greenbelt, Maryland 20771, USA. Fax: 1-301-286-0264 ------------------------------------------------------------ --------------