Replacing specific array elements in IDL

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 5K views
jf22901
Messages
55
Reaction score
1
Replacing specific array elements in IDL - solved!

Hi

I was just wondering if there was any way, other than using FOR loops, to replace specified elements of an array?

For example, say A is a 1D array of 50 elements, each equal to 1.0. Is it possible to change say elements 0-9 and 30-49 to 5.0 other than the way shown below? I thought maybe I could use the REPLICATE_INPLACE function, but can't find a way of getting it to work.

Code:
A = FLTARR(50)+1.0

FOR i = 0, 9 DO BEGIN
    A(i) = 5.0
ENDFOR

FOR i = 30, 49 DO BEGIN
    A(i) = 5.0
ENDFOR

END

The above method works fine, but there must be a way that doesn't involve loops, as I know loops slow IDL down.

Many thanks
 
Last edited:
Physics news on Phys.org
Problem solved. I've realized I can simply type A[0:9]=5.0 etc. :biggrin: