The EXTRAC function returns a defined portion of an array or vector. The main advantage to EXTRAC is that, when parts of the specified subsection lie outside the bounds of the array, zeros are returned for these outlying elements. It is usually more efficient to use the array subscript ranges (the “:” operator; see Array Subscript Ranges) to perform such operations.

EXTRAC was originally a built-in system procedure in the PDP-11 version of IDL, and was retained in that form in the original VAX/VMS IDL for compatibility. Most applications of the EXTRAC function are more concisely written using subscript ranges (e.g., X(10:15)). EXTRAC has been rewritten as a library function that provides the same interface as the previous versions.

Note: If you know that the subarray will never lie beyond the edges of the array, it is more efficient to use array subscript ranges (the “:” operator) to extract the data instead of EXTRAC.

This routine is written in the IDL language. Its source code can be found in the file in the lib subdirectory of the IDL distribution.


Extracting elements from a vector:

; Create a 1000 element floating-point vector with each element set 
; to the value of its subscript:
A = FINDGEN(1000)
; Extract 300 points starting at A[200] and extending to A[499]:
B = EXTRAC(A, 200, 300)

In the next example, the first 50 elements extracted (B[0] to B[49]) lie outside the bounds of the vector and are set to 0. The value of B[50] is the same as the value of A[0], and the value of B[51] is equal to A[1]which is 1. Enter:

; Create a 1000 element vector:
A = FINDGEN(1000)
; Extract 50 elements, 49 of which lie outside the bounds of A:
B = EXTRAC(A, -50, 100)

The following commands illustrate the use of EXTRAC with multi-dimensional arrays:

; Make a 64 by 64 array:
A = INTARR(64,64)
; Extract a 32 by 32 portion starting at A(20,30):
B = EXTRAC(A, 20, 30, 32, 32)

As suggested in the discussion above, a better way to perform the same operation as the previous line is:

; Use the array subscript operator instead of EXTRAC:
B = A(20:51, 30:61)

Extract the 20th column and 32nd row of A:

; Extract 20th column of A:
B = EXTRAC(A, 19, 0, 1, 64)
; Extract 32nd row of A:
B = EXTRAC(A, 0, 31, 64, 1)

Take a 32 BY 32 matrix from A starting at A(40,50):

; Note that those points beyond the boundaries of A are set to 0:
B = EXTRAC(A, 40, 50, 32, 32)


Result = EXTRAC( Array, C1, C2, ..., Cn, S1, S2, ..., Sn )

Return Value

Returns any rectangular sub-matrix or portion of the parameter array.



The array from which the subarray will be copied.


The starting subscript in Array for the subarray. There should be one Ci for each dimension of Array. These arguments must be integers.


The size of each dimension. The result will have dimensions of (S1, S2, ..., Sn). There should be one Si for each dimension of Array. These arguments must be non-negative.



Version History

Pre 4.0


See Also

Array Subscript Ranges