Idl Error Out Of Range Subscript Encountered
Contents |
Toolkit APIs ENVI API ESE API Libraries Astronomy Library Beaumont Library Bowman Library Catalyst Library Coyote Library Dáithí Library Galloy Library JBIU Library JHUAPLS1R Library Mankoff Library Markwardt Library Motley Library Salvaggio Library ENVI Using ENVI illegal subscript range idl Modules Atmospheric Correction DEM Extraction ENVI Photogrammetry Feature Extraction (FX) NITF ENVI API ESE Using
Attempt To Subscript Is Out Of Range Idl
ENVI Services Engine ENVI Services Engine API ENVI for ArcGIS@ Services Edition Resources Licensing PDF Guides Platform Support Harris Geospatial/Docs Center/Using IDL/Language/Variables/Assignment Operations and Arrays Assignment Operations and Arrays Assignment Operations and Arrays The following table shows the variations possible in expressions containing array and scalar subscripts. The result of the assignment operation depends upon the dimensionality of the subscript. Note: A subscript structure can also be composed of a range of elements. If expression is scalar, it is inserted into the subarray. If Variable[Range] and Array are the same size, elements of Array specified by Range are inserted in Variable. It is illegal if Variable[Range] and Array are different sizes. See Array Subscript Ranges for complete details. For information on when you should not use subscript ranges, see Avoid Using Range Subscripts for Assignment. Note: When assigning elements to an array, the elements are converted to the data type of the array variable. Depending upon the size of the values compared to the data type's range, this may result in the data wrapping around or losing precision. For example, assigning the value 257 to a byte array will result in the value 1. As another example, assigning any non-zero value to a boolean array will give the value true (1). Syntax Structure Description Variable[ScalarSubscripts] = ScalarExpression Expression is stored in a single element of Variable. arrOne = [1, 2, 3, 4, 5] arrOne[2] = 9 PRINT, arrOne 12945 Variable[ScalarSubscripts] = ArrayExpression Expression array is inserted in Variable array beginning at point indicated by subscript. arrOne = [1, 2, 3, 4, 5] arrTwo = [11, 12] arrOne[1] = ArrTwo PRINT, arrOne 1111245 Note: An “out of range subscript” error will occur if you attempt to insert arrTwo elements into non-existent elements of arrOne. For example arrOne[4] = ArrTwo fails. Variable[ArraySubscripts] = ScalarExpression Expression scalar is stored in designated elements of Variable. Other array elements are unchanged. arrOne = [1, 2, 3, 4, 5] arrOne[[2, 4]] = 0 PRINT, arrOne 12040 Note: Note the use of the
test = fltarr(2,2,4) IDL> testadd = fltarr(2,2,2) + 1 IDL> print, total(test), total(testadd) 0.00000 8.00000 IDL> test[0,0,1] += testadd IDL> print,total(test), total(testadd) 8.00000 8.00000 IDL> test[0,0,1] += testadd IDL> print, total(test), total(testadd) 16.0000 8.00000 IDL> print, test 0.00000 0.00000 0.00000 0.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000 0.00000 0.00000 0.00000 0.00000 That is exactly what I expect from this operation. But here is another case, with results that are somewhat different. IDL> test = fltarr(2,2,4) IDL> http://www.harrisgeospatial.com/docs/Assignment_Operations_an.html testadd = fltarr(2,2,2) IDL> testadd[0,0,*] = 1 IDL> print, total(test), total(testadd) 0.00000 2.00000 IDL> test[0,0,1] += testadd IDL> print, total(test), total(testadd) 2.00000 2.00000 IDL> print, test 0.00000 0.00000 0.00000 0.00000 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 ; So far, so good IDL> test[0,0,1] += testadd ; Now, let's add again IDL> print, total(test), http://www.idlcoyote.com/code_tips/lhsvsrhs.html total(testadd) 10.0000 2.00000 ; Huh? Say what!? IDL> print, test 0.00000 0.00000 0.00000 0.00000 2.00000 1.00000 1.00000 1.00000 2.00000 1.00000 1.00000 1.00000 0.00000 0.00000 0.00000 0.00000 ; Not even JD Smith could have expected this! Does anyone have any ideas? ANSWER: Naturally, JD had anticipated the question, and had a ready answer. He provides it here. Sure I did. In the first case, you are adding test[0,0,1], i.e. "0", to testadd, then setting the entire resulting 2x2x2 array en masse into test at offset [0,0,1]. In the second case, you are adding test[0,0,1], i.e. "1", to testadd, resulting in: 2 1 1 1 2 1 1 1 and then setting this resulting 2x2x2 array into test at offset [0,0,1]. What's surprising and perhaps nonintuitive here is the ambiguity between a single array index on the LHS of an assignment, and a single array index on the RHS. When on the RHS, an array element is treated simply as a scalar, and so is threaded across every element of any other arrays in the RHS calcu
the subscript. Note A subscript structure can also be composed of a range of elements. If expression is scalar, it is inserted into the subarray. If Variable[Range] and Array are the same size, elements http://northstar-www.dartmouth.edu/doc/idl/html_6.2/Assignment_Operations_and_Arrays.html of Array specified by Range are inserted in Variable. It is illegal if Variable[Range] and Array are different sizes. See Subscript Ranges for complete details. For information on when you should not use subscript ranges, see Avoid Using Range Subscripts. Table 15-2: Introduction to Subscript Expression Structures Table 15-2: Introduction to Subscript Expression Structures Syntax Structure Description Variable[ScalarSubscripts] = ScalarExpression Expression is stored in a single out of element of Variable. arrOne = [1, 2, 3, 4, 5] arrOne[2] = 9 PRINT, arrOne 12945 Variable[ScalarSubscripts] = ArrayExpression Expression array is inserted in Variable array beginning at point indicated by subscript. arrOne = [1, 2, 3, 4, 5] arrTwo = [11, 12] arrOne[1] = ArrTwo PRINT, arrOne 1111245 Note - An "out of range subscript" error will occur if you attempt to insert arrTwo elements out of range into non-existent elements of arrOne. For example arrOne[4] = ArrTwo fails. Variable[ArraySubscripts] = ScalarExpression Expression scalar is stored in designated elements of Variable. Other array elements are unchanged. arrOne = [1, 2, 3, 4, 5] arrOne[[2, 4]] = 0 PRINT, arrOne 12040 Note - Note the use of the double brackets. Attempting to assign zeros to the 3rd and 5th element of the array usingarrOne[2, 4] = 0 results in an error: "Attempt to subscript ARRONE with
has the same result as arrOne[[0,2]]. See Clipping for details. Note Array operations are much more efficient than loops. See Use Vector and Array Operations for details. IDL Online Help (June 16, 2005)