Creating Arrays

IDL provides a variety of mechanisms to create arrays. IDL arrays can have up to 8 dimensions. Each dimension can have a length between 1 and the maximum integer value (either a 32-bit integer for 32-bit IDL or a 64-bit integer for 64-bit IDL).

Array Concatenation

Using square brackets, you can combine scalars or arrays into a single array. For example:

a = [1,2,3,4,5]

b = [6,7,8,9,10]

c = [a, b]

print, c

IDL prints:

1 2 3 4 5 6 7 8 9 10

You can also mix scalars and arrays as long as the arrays are one-dimensional vectors:

d = [c, 11, a + 11, 17, 18, 19, 20, c + 20]

print, d

IDL prints:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

To concatenate second or higher levels, you can nest the brackets. For example, [[1,2,3],[4,5,6]] will create a 3-element by 2-element array with the first row containing 1, 2, 3 and the second row containing 4, 5, 6. The operands must have compatible dimensions: All dimensions must be equal except the dimension that is to be concatenated. For example:

; For concatenation to work, each row must have the same # of elements (5)

kernel1 = [[1,2,4,2,1], [2,4,8,4,2], [1,2,4,2,1]]

help, kernel1

print, kernel1

IDL prints:

KERNEL1 INT = Array[5, 3]

 

1 2 4 2 1

2 4 8 4 2

1 2 4 2 1

Now we can combine this kernel array with another array, along either the first or second dimension. The level of bracket nesting determines which dimension will be concatenated. For example:

kernel2 = [[-1,0,2,0,-1], [1,2,4,2,1], [-1,0,2,0,-1]]

kernelWide = [kernel1, kernel2] ; concatenate along first dimension

kernelTall = [[kernel1], [kernel2]] ; concatenate along second dimension

help, kernelWide

help, kernelTall

IDL prints:

KERNELWIDE INT = Array[10, 3]

KERNELTALL INT = Array[5, 6]

You can nest brackets up to 8 levels deep, which is the maximum number of dimensions for an IDL array. For example:

IDL> a = fltarr(2,2,2,2,2,2,2,2)

IDL> b = a

IDL> help, [a, b]

<Expression> FLOAT = Array[4, 2, 2, 2, 2, 2, 2, 2]

IDL> help, [[[[a]]], [[[b]]]]

<Expression> FLOAT = Array[2, 2, 2, 4, 2, 2, 2, 2]

IDL> help, [[[[[[[[a]]]]]]], [[[[[[[b]]]]]]]]

<Expression> FLOAT = Array[2, 2, 2, 2, 2, 2, 2, 4]

Data Type for Array Concatenation

When you use array concatenation, IDL will automatically choose the data type for the final array based upon the data types of the individual elements. Any elements which are not of the correct data type will be converted. For example:

a = [0.5, 1, 2, 3, 4] ; float with integers

help, a

b = [a, 5d] ; append a double-precision value

help, b

IDL prints:

A FLOAT = Array[5]

B DOUBLE = Array[6]

See Data Type and Structure of Expressions for the rules used when promoting types.

Array Creation Functions

The IDL functions for array creation fall into two groups:

The prefix in front of each function determines the result type. For example BYTARR returns byte arrays, while UINTARR

All of these functions have the same general syntax. For example:

Here, the D1[, ..., D8] represent the size of each dimension of the result. You can also specify the dimensions by providing an array for the first argument.

For the *ARR family, you can use the NOZERO keword to avoid setting each element to zero.

For the *INDGEN family, you can use the START and INCREMENT keywords to construct arrays that start at a non-zero value and have a spacing that is greater or smaller than 1.

For example:

Command

Result

print, FLTARR(5)

0.000000 0.000000 0.000000 0.000000 0.000000

help, FLTARR(360, 180, 10)

<Expression> FLOAT = Array[360, 180, 10]

print, INDGEN(10)

0 1 2 3 4 5 6 7 8 9

print, MAX(LINDGEN(1000, 100))

99999

print, INDGEN(5, START=10, INCREMENT=5)

10 15 20 25 30

print, INDGEN(10, INCREMENT=0.5)

0 0 1 1 2 2 3 3 4 4

print, FINDGEN(10, INCREMENT=0.5) 0.000000 0.500000 1.00000 1.50000 2.00000
2.50000 3.00000 3.50000 4.00000 4.50000

You can also use the following routines to create more specialized arrays:

See Array Creation for a complete list of routines.

Colon Operator

You can use the colon ":" operator to construct arrays with increasing or decreasing values. The colon operator provides a more readable, short-hand syntax for the *INDGEN family of functions.

The operator has the following syntax:

Result = [start: finish]

or

Result = [start: finish: increment]

With the first form (without an increment), the increment is assumed to be 1 if finish is greater than or equal to start, or -1 if finish is less than start. For example,

PRINT, [0:9]

PRINT, [9:0]

IDL prints:

0 1 2 3 4 5 6 7 8 9

9 8 7 6 5 4 3 2 1 0

With the second form (with an increment), you are responsible for making sure that the increment has the correct sign. For example, if finish is less than start then your increment should be negative.

Note: You will get a runtime error if increment is zero or is the wrong sign.

Note: If any of the arguments is equal to !NULL the result will also be !NULL.

Data Type for Array Concatenation

When you use the colon operator, IDL will automatically choose the data type for the final array based upon the data types of the arguments. For example:

a = [0: 1: 0.1] ; integers with a float stride

HELP, a

 

b = [0L: 10: 2] ; one of the integers is a long

HELP, b

IDL prints:

A FLOAT = Array[11]

B LONG = Array[6]

See Data Type and Structure of Expressions for the rules used when promoting types.

Examples

INDGEN Command

Colon Operator

Result

INDGEN(10)

[0:9]

0 1 2 3 4 5 6 7 8 9

9 - INDGEN(10)

[9:0]

9 8 7 6 5 4 3 2 1 0

INDGEN(10)

[0:9:1]

0 1 2 3 4 5 6 7 8 9

9 - INDGEN(10)

[9:0:-1]

9 8 7 6 5 4 3 2 1 0

10 + 5*INDGEN(5)

[10:30:5]

10 15 20 25 30

0.5*FINDGEN(7)

[0:3:0.5]

0.0 0.5 1.0 1.5 2.0 2.5 3.0

You can use the colon operator anywhere that you use an IDL expression. For example, you could combine array concatenation with the colon operator:

arr = [[1:5], [6:10], 11, [11:16], 17, 18, 19, 20, [21:30]]

HELP, arr

IDL prints:

ARR INT = Array[31]

You can create two and three-dimensional arrays by combining the colon operator with array concatenation. For example:

a = [[10:19], [20:29], [30:39]]

HELP, a

PRINT, a

IDL prints:

A INT = Array[10, 3]

 

10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29

30 31 32 33 34 35 36 37 38 39

You can also use the colon operator as the argument in a FOREACH statement:

FOREACH x,[0:1:0.2] DO PRINT, x

IDL prints:

0.000000

0.200000

0.400000

0.600000

0.800000

1.00000