Fundamentals of IDL syntax II: Arrays

Post on 02-Jan-2016

27 views 0 download

description

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 - PowerPoint PPT Presentation

Transcript of 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 • One of the secrets to becoming an

effective IDL programmer is learning how to use arrays effectively

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

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]

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

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]

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]

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

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]

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.