1 / 10

Fundamentals of IDL syntax II: Arrays

Fundamentals of IDL syntax II: Arrays. Introduction to Arrays. 1-8 dimensions Two most compelling reasons to use arrays in IDL 1. Array operations are much faster than loops 2. Array syntax is more compact than a corresponding loop construct

Download Presentation

Fundamentals of IDL syntax II: Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals of IDL syntax II: Arrays

  2. Introduction to Arrays • 1-8 dimensions • Two most compelling reasons to use arrays in IDL 1. Array operations are much faster than loops 2. Array syntax is more compact than a corresponding loop construct • One of the secrets to becoming an effective IDL programmer is learning how to use arrays effectively

  3. Creating arrays • Please try to create all the arrays at the beginning of your program • Arrays are created using square bracket [ ] x=[0, 1, 2, 3, 4] help, x print, x • Functions for creating zeroed arrays and index arrays • Use index array to create a grid array n=10 x0=5 dx=10 arr=x0+findgen(n)*dx print, arr

  4. Extract elements or subset of an array • Use array indices (also known as subscripts) • Note in IDL, index starts from 0 instead of 1 • Scalar index: print, arr[6] Index range: print, arr[0:3] All indices: print, arr[*] All indices from a specific index to the last index: print, arr[5:*] Indices given by a variable expression: I=3 print, arr[I-1:I+1] Indices specified by an array: index=indgen(3)*2 print, index print, arr[index]

  5. Initialize an array or its selected elements or subsets • All elements: Arr[*] = -9999.0 • One element: Arr[0] = 3.0 • Subset range: Arr[5:*] = 2.0 • Subset specified by an array index=indgen(3)*2 print, index arr[index] = 4.0

  6. Multi-dimensional array • Zeroed arrays and index arrays can be created using similar functions as those used for 1-D arrays arr = intarr(2, 3) arr = findgen(2, 3) • Arrays in IDL are stored in column-major format (same as FORTRAN), which means that consective array elements are stored sequentially in memory, with the first array dimension (the column) varying the fastest. a[0,0], a[1,0], a[2,0], a[0,1], a[1,1], a[2,1]

  7. Multi-dimensional array indexing • Similar to 1-D array • One element: print, arr[0,0] • All elements in a row: print, arr[*,0] • All elements in a column: print, arr[0,*] • Index range over two dimensions: print, arr[0:3,1:2]

  8. Array properties • moment(arr) returns an array, with the first element being the mean, second being the variance, third being the skew, … • Two other useful functions: median, sort

  9. Locating values within an array • Use the function “where” arr = indgen(10)*2 print, arr index = where(arr gt 5) print, index print, arr[index]

  10. In-class assignment II • Create a 1-D grid array with 20 elements, and with the first value to be 3 and increment to be 2. (1) print out the array (2) calculate the mean, max, min, variance, and standard deviation of the array. Print out the results. (3) print out the elements smaller than 8 (4) print out the elements not smaller than 10 (5) change the value of all elements from the 5th to the last to be -9999.0, then repeat steps 1-4. • Create a 2-D index array (dimensions 2*4). Repeat the above steps. For step (5), change the value of all elements from the 5th to the last index in the first column to be -9999.0.

More Related