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

Home » Public Forums » archive » Re: subarray
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: subarray [message #59402] Wed, 19 March 2008 03:30
Wox is currently offline  Wox
Messages: 184
Registered: August 2006
Senior Member
On Wed, 19 Mar 2008 03:19:09 -0700 (PDT), Gaurav
<selfishgaurav@gmail.com> wrote:

> On Mar 19, 1:59�pm, Wox <nom...@hotmail.com> wrote:
>> I've seen this somewhere but I can't find it: I want to take a
>> subarray of an array by giving the number of elements for each
>> dimension. When the original array is to small for one or more
>> dimensions: append zero's, otherwise take the range 0:n-1.
>
>
> 'EXTRAC' should be your tool of choice! Look up the usage in help.
>
> Cheers
>
> Gaurav

That's it! I knew I saw it somewhere. Thanks.
Re: subarray [message #59403 is a reply to message #59402] Wed, 19 March 2008 03:19 Go to previous message
Gaurav is currently offline  Gaurav
Messages: 50
Registered: January 2007
Member
On Mar 19, 1:59 pm, Wox <nom...@hotmail.com> wrote:
> I've seen this somewhere but I can't find it: I want to take a
> subarray of an array by giving the number of elements for each
> dimension. When the original array is to small for one or more
> dimensions: append zero's, otherwise take the range 0:n-1.


'EXTRAC' should be your tool of choice! Look up the usage in help.

Cheers

Gaurav
Re: subarray [message #59405 is a reply to message #59403] Wed, 19 March 2008 02:29 Go to previous message
Wox is currently offline  Wox
Messages: 184
Registered: August 2006
Senior Member
On Wed, 19 Mar 2008 09:59:32 +0100, Wox <nomail@hotmail.com> wrote:

> Hi all,
>
> I've seen this somewhere but I can't find it: I want to take a
> subarray of an array by giving the number of elements for each
> dimension. When the original array is to small for one or more
> dimensions: append zero's, otherwise take the range 0:n-1.
>
> Thanks,
>
> Wout

To clarify what I want, see below. This code seems a bit clumsy
though. Somehow I seem to remember there was a better way...



function subarray,array,dims

; Check new size
ndims=n_elements(dims)
if ndims eq 0 or ndims gt 8 then return,size(dummy)
dims>=1

; Check old size
if n_elements(array) eq 0 then array=0
s=size([array],/dim)
if n_elements(s) lt ndims then s=[s,lonarr(ndims-n_elements(s))]

; Shrink array
switch ndims of
8: if dims[7] lt s[7] then array=array[*,*,*,*,*,*,*,0:dims[7]-1]
7: if dims[6] lt s[6] then array=array[*,*,*,*,*,*,0:dims[6]-1,*]
6: if dims[5] lt s[5] then array=array[*,*,*,*,*,0:dims[5]-1,*,*]
5: if dims[4] lt s[4] then array=array[*,*,*,*,0:dims[4]-1,*,*,*]
4: if dims[3] lt s[3] then array=array[*,*,*,0:dims[3]-1,*,*,*,*]
3: if dims[2] lt s[2] then array=array[*,*,0:dims[2]-1,*,*,*,*,*]
2: if dims[1] lt s[1] then array=array[*,0:dims[1]-1,*,*,*,*,*,*]
1: if dims[0] lt s[0] then array=array[0:dims[0]-1,*,*,*,*,*,*,*]
endswitch

; Expand array
temp=temporary(array)
array=make_array(dimension=dims,value=temp[0])

case ndims of
1: array[0]=temporary(temp)
2: array[0,0]=temporary(temp)
3: array[0,0,0]=temporary(temp)
4: array[0,0,0,0]=temporary(temp)
5: array[0,0,0,0,0]=temporary(temp)
6: array[0,0,0,0,0,0]=temporary(temp)
7: array[0,0,0,0,0,0,0]=temporary(temp)
8: array[0,0,0,0,0,0,0,0]=temporary(temp)
endcase

; Return size
return,size(array)
end;function subarray
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: widget_text, CW_FIELD
Next Topic: Shapefile 'parts' woes

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

Current Time: Wed Oct 08 15:15:40 PDT 2025

Total time taken to generate the page: 0.00570 seconds