Replacing specific array elements in IDL

AI Thread 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:
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top