comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: null array
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: null array [message #36471] Wed, 24 September 2003 18:35 Go to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
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
Re: null array [message #36472 is a reply to message #36471] Wed, 24 September 2003 17:47 Go to previous messageGo to next message
tomson is currently offline  tomson
Messages: 39
Registered: March 2003
Member
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
>
Re: null array [message #36478 is a reply to message #36472] Wed, 24 September 2003 09:17 Go to previous messageGo to next message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
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
Re: null array [message #36479 is a reply to message #36478] Wed, 24 September 2003 08:11 Go to previous messageGo to next message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
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
============================================================ =======
Re: null array [message #36480 is a reply to message #36479] Wed, 24 September 2003 06:52 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
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
Re: null array [message #36481 is a reply to message #36480] Wed, 24 September 2003 02:08 Go to previous messageGo to next message
Ben Panter is currently offline  Ben Panter
Messages: 102
Registered: July 2003
Senior Member
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.
Re: null array [message #36482 is a reply to message #36481] Wed, 24 September 2003 01:33 Go to previous messageGo to next message
Ben Panter is currently offline  Ben Panter
Messages: 102
Registered: July 2003
Senior Member
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!
Re: null array [message #36483 is a reply to message #36482] Tue, 23 September 2003 20:21 Go to previous messageGo to next message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
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
Re: null array [message #36484 is a reply to message #36483] Tue, 23 September 2003 20:04 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
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
Re: null array [message #36544 is a reply to message #36471] Tue, 30 September 2003 12:36 Go to previous message
Stein Vidar Hagfors H[2] is currently offline  Stein Vidar Hagfors H[2]
Messages: 28
Registered: October 2002
Junior Member
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
------------------------------------------------------------ --------------
Re: null array [message #36562 is a reply to message #36478] Fri, 26 September 2003 02:24 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to read the data like this?
Next Topic: Programming ENVI

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:10:22 PDT 2025

Total time taken to generate the page: 0.00807 seconds