The old Google Groups will be going away soon, but your browser is incompatible with the new version.
For loop avoidance - getting indices of real space
 There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic. There was an error processing your request. Please try again. Standard view   View as tree
 8 messages

From:
To:
Cc:
Followup To:
Subject:
 Validation: For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon.

More options Aug 23 2012, 4:58 pm
Newsgroups: comp.lang.idl-pvwave
From: simul...@gmail.com
Date: Thu, 23 Aug 2012 13:58:48 -0700 (PDT)
Local: Thurs, Aug 23 2012 4:58 pm
Subject: For loop avoidance - getting indices of real space

And yet, I still can't quite grasp at how I can solve my for loop problem.  I think it might involve the use of modulo (MOD), but I'm not sure how.  My question is, how can you grab the indices (i,j,k) of a 3D array in real space, and throw them into basically 3 1D arrays that is just a list of all the cells in the "proper" order (column-major).

Here is an example of what I mean:

xcells=15
ycells=10
zcells=20
ncells=xcells*ycells*zcells

data=dindgen(xcells,ycells,zcells)
coord=intarr(ncells,3)

index=0L
for k=0,zcells do begin
for j=0,ycells do begin
for i = 0,xcells do begin
coord(index,0)=i
coord(index,1)=j
coord(index,2)=k
index=index+1
endfor
endfor
endfor

end

This is a really simple version of a complex problem I have.  I have sets of different size boxes from an AMR MHD code, and I need to keep track of their indices, but I just want a list of all of the cells, not to drag around a bunch of smaller arrays or try to concatenate them into one giant sparse array (waste of space).  I'm certain that someone must have had this problem before, but I can't find any other suggestions on this forum.

To post a message you must first join this group.
You do not have the permission required to post.
More options Aug 24 2012, 5:32 am
Newsgroups: comp.lang.idl-pvwave
Date: Fri, 24 Aug 2012 02:32:05 -0700 (PDT)
Local: Fri, Aug 24 2012 5:32 am
Subject: Re: For loop avoidance - getting indices of real space
Le jeudi 23 août 2012 22:58:48 UTC+2, simu...@gmail.com a écrit :

If I understand well your problem, a solution might be:

IDL> coord = [ [lindgen(xcells)#replicate(1,ycells*zcells)], \$
IDL>   [lindgen(ycells)#replicate(1,xcells*zcells)], \$
IDL>   [lindgen(zcells)#replicate(1,xcells*ycells)] ]
IDL> coord = reform(coord,ncells,3,/OVER)

alain.

To post a message you must first join this group.
You do not have the permission required to post.
More options Aug 24 2012, 10:25 am
Newsgroups: comp.lang.idl-pvwave
From: simul...@gmail.com
Date: Fri, 24 Aug 2012 07:25:03 -0700 (PDT)
Local: Fri, Aug 24 2012 10:25 am
Subject: Re: For loop avoidance - getting indices of real space

This seems like a great idea, but IDL won't let me concatenate arrays like this.  The above produces the error message:

% Unable to concatenate variables because the dimensions do not agree: <LONG    Array[10,300]>.

Can you think of another way to form those coordinates?

Thanks,

Christina

To post a message you must first join this group.
You do not have the permission required to post.
More options Aug 24 2012, 10:42 am
Newsgroups: comp.lang.idl-pvwave
From: simul...@gmail.com
Date: Fri, 24 Aug 2012 07:42:23 -0700 (PDT)
Local: Fri, Aug 24 2012 10:42 am
Subject: Re: For loop avoidance - getting indices of real space

On Friday, August 24, 2012 5:32:05 AM UTC-4, alx wrote:
> Le jeudi 23 août 2012 22:58:48 UTC+2, simu...@gmail.com a écrit :
> If I understand well your problem, a solution might be:

> IDL> coord = [ [lindgen(xcells)#replicate(1,ycells*zcells)], \$

> IDL>   [lindgen(ycells)#replicate(1,xcells*zcells)], \$

> IDL>   [lindgen(zcells)#replicate(1,xcells*ycells)] ]

> IDL> coord = reform(coord,ncells,3,/OVER)

> alain.

Although it may not work exactly as described here, I think I can see a version that would work, albeit less elegantly.

If I just go for each of them individually, like so:

coordx=lindgen(xcells)#replicate(1,ycells*zcells)
coord(ncells,0)=reform(coordx,ncells,1,/OVER)

etc., I think it does work!  Thanks!

To post a message you must first join this group.
You do not have the permission required to post.
More options Aug 24 2012, 10:41 am
Newsgroups: comp.lang.idl-pvwave
Date: Fri, 24 Aug 2012 07:41:10 -0700 (PDT)
Local: Fri, Aug 24 2012 10:41 am
Subject: Re: For loop avoidance - getting indices of real space
Le vendredi 24 août 2012 16:25:03 UTC+2, simu...@gmail.com a écrit :

Sorry, I missed one step: I should have rather written:

IDL> coord = [ reform([lindgen(xcells)#replicate(1,ycells*zcells),ncells]), \$
IDL>   reform([lindgen(ycells)#replicate(1,xcells*zcells)],ncells), \$
IDL>   reform([lindgen(zcells)#replicate(1,xcells*ycells)],ncells) ]

alain.

To post a message you must first join this group.
You do not have the permission required to post.
More options Aug 27 2012, 6:37 am
Newsgroups: comp.lang.idl-pvwave
From: Yngvar Larsen <larsen.yng...@gmail.com>
Date: Mon, 27 Aug 2012 03:37:34 -0700 (PDT)
Local: Mon, Aug 27 2012 6:37 am
Subject: Re: For loop avoidance - getting indices of real space

On Thursday, 23 August 2012 22:58:48 UTC+2, simu...@gmail.com  wrote:

> And yet, I still can't quite grasp at how I can solve my for loop problem.  I think it might involve the use of modulo (MOD), but I'm not sure how.  My question is, how can you grab the indices (i,j,k) of a 3D array in real space, and throw them into basically 3 1D arrays that is just a list of all the cells in the "proper" order (column-major).

There is a perfectly good builtin function in IDL to do this: ARRAY_INDICES.

IDL> nx = 15L
IDL> ny = 10L
IDL> nz = 20L
IDL> data = randomn(seed, nx,ny,nz)
IDL> ai = array_indices(data, lindgen(nx*ny*nz))
IDL>
IDL> help, ai
AI              LONG      = Array[3, 3000]

If you really need dimensions [3000,3], you can add
IDL> ai = transpose(temporary(ai))

--
Yngvar

To post a message you must first join this group.
You do not have the permission required to post.
More options Aug 28 2012, 5:41 pm
Newsgroups: comp.lang.idl-pvwave
From: JDS <jdtsmith.nos...@yahoo.com>
Date: Tue, 28 Aug 2012 14:41:28 -0700 (PDT)
Local: Tues, Aug 28 2012 5:41 pm
Subject: Re: For loop avoidance - getting indices of real space

On Thursday, August 23, 2012 4:58:48 PM UTC-4, simu...@gmail.com wrote:

Just for the record, that particular post was made almost entirely in jest: sometimes a for loop is indeed what you want.

JD

To post a message you must first join this group.
You do not have the permission required to post.
More options Sep 21 2012, 4:06 pm
Newsgroups: comp.lang.idl-pvwave
From: simul...@gmail.com
Date: Fri, 21 Sep 2012 13:06:04 -0700 (PDT)
Local: Fri, Sep 21 2012 4:06 pm
Subject: Re: For loop avoidance - getting indices of real space

On Tuesday, August 28, 2012 5:41:29 PM UTC-4, JDS wrote:
> On Thursday, August 23, 2012 4:58:48 PM UTC-4, simu...@gmail.com wrote:

> > I have read and re-read until cross-eyed this post:  http://www.idlcoyote.com/tips/forloops.html

> Just for the record, that particular post was made almost entirely in jest: sometimes a for loop is indeed what you want.

> JD

*Almost* entirely you say, but in my case entirely accurate.  I reduced the runtime of my code from 10 minutes to 2 minutes by using the above arrays to find my indices instead of for loops, which seems significant to me, considering I need to run it over and over again.  I was getting really bored.

In general I find that it's easy to loose focus on a problem if your runtime is approximately between 4 min - 2 hours.  You feel like it's too short of a time to switch focus to another project and be productive at all.  So, yay for code optimization.

In response to Yngvar:

I also had the problem that I had to add a constant value to each i,j,k specified by another array xBounds that gives the lower left corner of each grid.  Since I was basically concatenating all the data from each grid, I could figure out how to add this lower left corner value to each data point before the reform.