|
Re: (more) Efficient way to generate an array whose elements are the distance from the center [message #80702 is a reply to message #80701] |
Fri, 06 July 2012 13:53   |
Lajos Foldy
Messages: 176 Registered: December 2011
|
Senior Member |
|
|
On Friday, July 6, 2012 10:37:35 PM UTC+2, Mike F. wrote:
> Hello all.
>
> I'm new to IDL (and coding in general), and I'm looking to find a more efficient way to generate an nxn array where each element is the distance from the center of the array.
>
> 3 x 3 Ex: 1.4 1 1.4
> 1 0 1
> 1.4 1 1.4
>
> All I can think of on my own is a nested FOR loop as such:
>
> FOR i = 0l, (n - 1) DO BEGIN
> FOR j = 0l, (n - 1) DO BEGIN
>
> plane[i,j] = SQRT( (i-n/2.)^2 + (j - n/2.)^2 )
>
> ENDFOR
> ENDFOR
>
> From what I've read on IDL forums, nested FOR loops are the pinnacle of sin, and I'd like to be a bit more pious if possible.
>
> Any tips would be appreciated!
Look up DIST and SHIFT in the docs:
IDL> print, shift(dist(3),1,1)
1.41421 1.00000 1.41421
1.00000 0.00000 1.00000
1.41421 1.00000 1.41421
regards,
Lajos
|
|
|
Re: (more) Efficient way to generate an array whose elements are the distance from the center [message #80794 is a reply to message #80702] |
Sun, 08 July 2012 08:39  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 7/6/12 2:53 PM, fawltylanguage@gmail.com wrote:
> On Friday, July 6, 2012 10:37:35 PM UTC+2, Mike F. wrote:
>> Hello all.
>>
>> I'm new to IDL (and coding in general), and I'm looking to find a more efficient way to generate an nxn array where each element is the distance from the center of the array.
>>
>> 3 x 3 Ex: 1.4 1 1.4
>> 1 0 1
>> 1.4 1 1.4
>>
>> All I can think of on my own is a nested FOR loop as such:
>>
>> FOR i = 0l, (n - 1) DO BEGIN
>> FOR j = 0l, (n - 1) DO BEGIN
>>
>> plane[i,j] = SQRT( (i-n/2.)^2 + (j - n/2.)^2 )
>>
>> ENDFOR
>> ENDFOR
>>
>> From what I've read on IDL forums, nested FOR loops are the pinnacle of sin, and I'd like to be a bit more pious if possible.
>>
>> Any tips would be appreciated!
>
> Look up DIST and SHIFT in the docs:
>
> IDL> print, shift(dist(3),1,1)
> 1.41421 1.00000 1.41421
> 1.00000 0.00000 1.00000
> 1.41421 1.00000 1.41421
>
> regards,
> Lajos
>
Also, DIST is written in dist.pro, so check it out in lib/ to look at
how to do this.
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL, A Guide to Learning IDL: http://modernidl.idldev.com
Research Mathematician
Tech-X Corporation
|
|
|