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

Home » Public Forums » archive » Re: using the WHERE function on a portion of an 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: using the WHERE function on a portion of an array [message #59029] Tue, 04 March 2008 12:15 Go to next message
Jean H. is currently offline  Jean H.
Messages: 472
Registered: July 2006
Senior Member
> I thought this could be done via a WHERE function call, such as:
> indices = WHERE(A[0,*,*] ge 4 AND A[0,*,*] lt 5, count)

so, indices refers to A[0,*,*], which is a 2D array.


> if count gt 0 then C[4,indices] = B[0,indices]

Now you are try to apply your 2D array in a 3D one, which can not work
properly.
To access your 3D array, you must either have a 3D index, or have a 1D
index.

So in your case, you want to write in C, on the 5th plane:
indices1D_C = indices + (n_elements(C[0,*,*]) * 4
And you want to read B on the 1st plane:
indices1D_B = indices

and then C[indices1D_C] = B[indices1D_B]


Jean

> On the other hand, if I set each level I am looking at to its own 2d
> array, i.e.,
> leva = A[0,*,*]
> levb = B[0,*,*]
> levc = C[4,*,*]
> use these values in the same code written above, and add the statement
> at the end that C[4,*,*] = levc, then it works just fine. However, A
> and B are actually very large, so this isn't an option.
>
> I'm guessing I do not understand some key part of the WHERE function.
> Would someone please shine some light on this for me? Thanks in
> advance.
> Becky
Re: using the WHERE function on a portion of an array [message #59032 is a reply to message #59029] Tue, 04 March 2008 11:39 Go to previous messageGo to next message
greg.addr is currently offline  greg.addr
Messages: 160
Registered: May 2007
Senior Member
On Mar 4, 8:38 pm, greg.a...@googlemail.com wrote:
> On Mar 4, 8:23 pm, becky_s <rda.se...@gmail.com> wrote:
>
>
>
>> Dear all,
>
>> Please lend me your great expertise to help me solve this problem I
>> have with the WHERE function.
>
>> I have a 3d array of heights, A, and another 3d array of observations
>> at those heights, B. I have a third 3d array, C. I would like
>> C[0,*,*] to contain values of B only if the corresponding value of A
>> is between 0 and 1; C[1,*,*] would have values of B only if 1<=A<2,
>> etc.
>
>> I thought this could be done via a WHERE function call, such as:
>> indices = WHERE(A[0,*,*] ge 4 AND A[0,*,*] lt 5, count)
>> if count gt 0 then C[4,indices] = B[0,indices]
>
>> but this does not work. Printing A[0,indices], I can see that these
>> values are not b/w 4 and 5.
>
>> On the other hand, if I set each level I am looking at to its own 2d
>> array, i.e.,
>> leva = A[0,*,*]
>> levb = B[0,*,*]
>> levc = C[4,*,*]
>> use these values in the same code written above, and add the statement
>> at the end that C[4,*,*] = levc, then it works just fine. However, A
>> and B are actually very large, so this isn't an option.
>
>> I'm guessing I do not understand some key part of the WHERE function.
>> Would someone please shine some light on this for me? Thanks in
>> advance.
>> Becky
>
> If I've understood your problem correctly, I'd make one more array to
> use for your comparisons:
>
> sz=size(A)
> d=rebin(findgen(sz[0]),sz[0],sz[1],sz[2])
>
> and then do the whole job in one step:
>
> q=where((A ge d) and (A lt d+1.))
> C[q]=B[q]
>
> regards,
> Greg

Sorry, that should be:

sz=size(A,/dim)
Re: using the WHERE function on a portion of an array [message #59033 is a reply to message #59032] Tue, 04 March 2008 11:38 Go to previous messageGo to next message
greg.addr is currently offline  greg.addr
Messages: 160
Registered: May 2007
Senior Member
On Mar 4, 8:23 pm, becky_s <rda.se...@gmail.com> wrote:
> Dear all,
>
> Please lend me your great expertise to help me solve this problem I
> have with the WHERE function.
>
> I have a 3d array of heights, A, and another 3d array of observations
> at those heights, B. I have a third 3d array, C. I would like
> C[0,*,*] to contain values of B only if the corresponding value of A
> is between 0 and 1; C[1,*,*] would have values of B only if 1<=A<2,
> etc.
>
> I thought this could be done via a WHERE function call, such as:
> indices = WHERE(A[0,*,*] ge 4 AND A[0,*,*] lt 5, count)
> if count gt 0 then C[4,indices] = B[0,indices]
>
> but this does not work. Printing A[0,indices], I can see that these
> values are not b/w 4 and 5.
>
> On the other hand, if I set each level I am looking at to its own 2d
> array, i.e.,
> leva = A[0,*,*]
> levb = B[0,*,*]
> levc = C[4,*,*]
> use these values in the same code written above, and add the statement
> at the end that C[4,*,*] = levc, then it works just fine. However, A
> and B are actually very large, so this isn't an option.
>
> I'm guessing I do not understand some key part of the WHERE function.
> Would someone please shine some light on this for me? Thanks in
> advance.
> Becky

If I've understood your problem correctly, I'd make one more array to
use for your comparisons:

sz=size(A)
d=rebin(findgen(sz[0]),sz[0],sz[1],sz[2])

and then do the whole job in one step:

q=where((A ge d) and (A lt d+1.))
C[q]=B[q]

regards,
Greg
Re: using the WHERE function on a portion of an array [message #59121 is a reply to message #59029] Tue, 04 March 2008 14:17 Go to previous message
becky_s is currently offline  becky_s
Messages: 4
Registered: February 2008
Junior Member
On Mar 4, 2:15 pm, Jean H <jghas...@DELTHIS.ucalgary.ANDTHIS.ca>
wrote:

> Now you are try to apply your 2D array in a 3D one, which can not work
> properly.
> To access your 3D array, you must either have a 3D index, or have a 1D
> index.
>
> So in your case, you want to write in C, on the 5th plane:
> indices1D_C = indices + (n_elements(C[0,*,*]) * 4
> And you want to read B on the 1st plane:
> indices1D_B = indices
>
> and then C[indices1D_C] = B[indices1D_B]
>

Jean,
Well, that is pretty slick! I knew there had to be some problem with
all my 2d to 3d dimension switching I was doing.

I did have to modify your solution somewhat, though. I ended up with
(I also generalized my previous code somewhat):

indices = WHERE(A[i,*,*] ge j AND A[i,*,*] lt (j+1), count)
if count gt 0 then begin
indices1D_C = indices*n_elements(C[*,0,0]) + j
indices1D_B = indices*n_elements(A[*,0,0]) + i
C[indices1D_C] = B[indices1D_B]
endif

Thanks again.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Expensive loops... can they be avoided?
Next Topic: itools (mouse) events

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

Current Time: Wed Oct 08 13:34:45 PDT 2025

Total time taken to generate the page: 0.00563 seconds