The SIZE function returns a vector that contains information indicating the size and type of the parameter. The returned vector is always of longword type.
The data type can also be returned by setting the TYPE keyword to SIZE. In this case, the return value of the SIZE function is the data type code of the given expression.
Assume A is an integer array with dimensions of (3,4,5). The statements:
arr = INDGEN(3,4,5) S = SIZE(arr)
assign to the variable S a six-element vector containing:
|
Element
|
Value
|
Description
|
|---|---|---|
| S0 | 3 |
Three dimensions
|
| S1 | 3 |
First dimension
|
| S2 | 4 |
Second dimension
|
| S3 | 5 |
Third dimension
|
| S4 | 2 |
Integer type
|
| S5 | 60 |
Number of elements = 3*4*5
|
The following code segment checks to see if the variable arr is two-dimensional and extracts the dimensions:
;Create a variable. arr = [[1,2,3],[4,5,6]] ;Get size vector. S = SIZE(arr) ;Check if two dimensional. IF S[0] NE 2 THEN $ ;Print error message. MESSAGE, 'Variable a is not two dimensional.' ;Get number of columns and rows. NX = S[1] & NY = S[2] PRINT, 'Array is ', NX, ' columns by ', NY, ' rows.'
IDL prints:
Array is 3 columns by 2 rows.
The following example illustrates two ways in which to determine the type code of the input expression.
The first method requires you to access the correct element of the array returned by the SIZE function (the second to last element). For example:
array = [[1,2,3], [4,5,6], [7,8,9]] sz = SIZE(array) type = sz[3] ;A more flexible method: sz = SIZE(array) n = N_ELEMENTS(sz) type = sz[n-2]
The second method involves using the TYPE keyword to SIZE. In this case, the value returned by the SIZE function contains only the type code of the input expression:
type = SIZE(array, /TYPE)