Programmer Guide > Using Subscripts with Arrays > Storing Elements with Array Subscripts
  

Storing Elements with Array Subscripts
One or more values may be stored in selected elements of an array by using an array expression as a subscript for the array variable appearing on the left side of an assignment statement. Values are taken from the expression on the right side of the assignment statement and stored in the elements whose subscripts are given by the array subscript. The right-hand expression may be either a scalar or array.
See "Assignment Statement" for details and examples of storing with vector subscripts.
Examples
A([2, 4, 6]) = 0
zeroes elements A(2), A(4), and A(6), without changing other elements of A. The following statement:
A([2, 4, 6]) = [4, 16, 36]
is equivalent to the statements:
A(2) = 4
A(4) = 16
A(6) = 36
One way to create a square n-by-n identity matrix is:
A = FLTARR(N, N)
A(INDGEN(N)*(N + 1)) = 1.0
The expression INDGEN(N)*(N + 1) results in a vector containing the 1D subscripts of the diagonal elements. Yet another way is to use two array subscripts:
A = FLTARR(N, N)
A(INDGEN(N), INDGEN(N)) = 1.0
The statement:
A(WHERE(A LT 0)) = -1
sets negative elements of A to minus 1.
Consider also the following examples:
a = INTARR( 4, 4, 4 ) & a([0,2,3],[1,3],0:1) = 10 & PM, a 
 
0
0
0
0
10
0
10
10
0
0
0
0
10
0
10
10
0
0
0
0
10
0
10
10
0
0
0
0
10
0
10
10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
a = INTARR( 4, 4 ) & a([0,2,3],[1,3],0) = INDGEN(6) & PM, a 
 
0
0
0
3
0
0
0
0
0
1
0
4
0
2
0
5
a = INTARR( 4, 4 ) & a(INDGEN(4), INDGEN(4)) = 10 & PM, A 
 
10
0
0
0
0
10
0
0
0
0
10
0
0
0
0
10

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.