Replacing specific array elements in IDL

Click For Summary
The discussion focuses on optimizing array element replacement in IDL without using FOR loops. The initial query seeks alternatives to modify specific elements of a 1D array, specifically changing elements 0-9 and 30-49 from 1.0 to 5.0. The user initially demonstrates a loop-based approach but expresses concern about performance. The solution is discovered by using array slicing, allowing direct assignment to the specified indices, such as A[0:9] = 5.0, which effectively replaces the elements without the need for loops, enhancing efficiency in IDL programming.
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:
Technology news on Phys.org
Problem solved. I've realized I can simply type A[0:9]=5.0 etc. :biggrin:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
Replies
2
Views
2K
Replies
43
Views
5K
Replies
20
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
235
Views
14K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 12 ·
Replies
12
Views
14K