IDL Exercise 3
The purpose of this exercise is familiarization with arrays and array construction.
 
  1. Start the IDL development environment by typing idlde at the Unix command prompt.
  2. Open a new document. This can be done by selecting NEW under the FILE menu.

  3. Write the following commands into the file. When you are done, save it with the name prog3.pro  in the idl/programs directory in your account.

    a1=indgen(5)
    HELP,a1
    PRINT,a1

  4. Run the program by typing @prog3 at the IDL prompt. The help information about the array and the values of the array should be printed in the IDL response window. (If the program does not run then there is a problem with the way your directories have been set up or with the search path in IDL. The information to set things up is in Exercise 2.)
  5. You should get the following response:

    IDL> @prog3
    A1              INT       = Array[5]
           0       1       2       3       4

    You have constructed an array of integers with 1 row and 5 columns. The entries of the array are set equal to the array index. This counting scheme is very useful as the foundation for many operations with numbers. We will explore a few of these as we go.
     

  6. Let us now construct a 2D array that is filled with the index entries. Edit the file so that it now reads as shown below. Save it and then run it with prog3.pro.

  7. a1=indgen(4,5)
    HELP,a1
    PRINT,a1

    The response should be like that shown below.
     
     

    IDL> @prog3
    A1              INT       = Array[4, 5]
           0       1       2       3
           4       5       6       7
           8       9      10      11
          12      13      14      15
          16      17      18      19

    What information about the function INDGEN can you get from the above display? For example, the first index specifies the number of columns and the second index specifies the number of rows. The total number of elements is ____? The index runs along the ____ from ____ to _____.

  8. Let us now do an operation on the array. Suppose we wanted to multiply every element by 5 and subtract 50. We could edit our program so it reads as shown below. Do this and then run the program.
  9. a1=indgen(4,5)
    HELP,a1
    PRINT,a1

    b1=5*a1-50
    PRINT
    PRINT,'The result of 5*a1-50 is:'
    PRINT,b1

    IDL> @prog3
    A1              INT       = Array[4, 5]
           0       1       2       3
           4       5       6       7
           8       9      10      11
          12      13      14      15
          16      17      18      19

    The result of 5*a1-50 is:
         -50     -45     -40     -35
         -30     -25     -20     -15
         -10      -5       0       5
          10      15      20      25
          30      35      40      45

    The four lines that have been added do the computation and then print out the results. We have printed a blank line and a heading so we can keep track of the results. Take a close look at the results and be sure that you can explain the entries. Note how simple it was to do a fairly complicated computation.
     

  10. We will now demonstrate a method to select a portion of an array. Modify your program so it now selects a portion of the a1 and b1 arrays and prints out the results. We do this by using a statement like W=A[c1:c2,r1:r2] to select the portion from column c1 to column c2 and from row r1 to row r2 from array A and put it into an array W.
  11. a1=indgen(4,5)
    HELP,a1
    PRINT,a1

    b1=5*a1-50
    PRINT
    PRINT,'The result of 5*a1-50 is:'
    PRINT,b1

    a2=a1[2:3,0:3]
    b2=b1[2:3,0:3]

    PRINT
    PRINT,'Selection from a1'
    PRINT,a2
    PRINT
    PRINT,'Selection from b1'
    PRINT,b2
     

    IDL> @prog3
    A1              INT       = Array[4, 5]
           0       1       2       3
           4       5       6       7
           8       9      10      11
          12      13      14      15
          16      17      18      19

    The result of 5*a1-50 is:
         -50     -45     -40     -35
         -30     -25     -20     -15
         -10      -5       0       5
          10      15      20      25
          30      35      40      45

    Selection from a1
           2       3
           6       7
          10      11
          14      15

    Selection from b1
         -40     -35
         -20     -15
           0       5
          20      25

    Look carefully at the region of a1 and b1 that were selected. Relate these selections to the row and column indexes of the selection statement.

  12. A second kind of index is commonly used in addressing portions of an array. This index counts from the beginning of the array to the end. This is shown in array a1. We can use this index to select portions of an array. We will construct an index k=[2,3,6,7,10,11,14,15] and use it to do a selection. Modify your program by adding the following statements at the end. The results now contain same data but it is listed as a row vector. IDL keeps track of the data in two ways: as a 2D array and as a list of values. (You may have to make your browser window wide to see the entire output vector in one row.)
  13. k=[2,3,6,7,10,11,14,15]
    PRINT
    PRINT,'k selection from b1'
    PRINT,b1[k]
     

    IDL> @prog3
    A1              INT       = Array[4, 5]
           0       1       2       3
           4       5       6       7
           8       9      10      11
          12      13      14      15
          16      17      18      19

    The result of 5*a1-50 is:
         -50     -45     -40     -35
         -30     -25     -20     -15
         -10      -5       0       5
          10      15      20      25
          30      35      40      45

    Selection from a1
           2       3
           6       7
          10      11
          14      15

    Selection from b1
         -40     -35
         -20     -15
           0       5
          20      25

    k selection from b1
         -40     -35     -20     -15       0       5      20      25
    Converting between column,row) indexes and array indexes: Suppose that the column and row indexes of a particular array location are (C,R) and that the array has NC columns. Then, as you can verify, the array index is  k=C+R*NC. This gives us a way to calculate the array index if we are given column and row indexes. Let us test this by constructing the column and row indexes for the example above and then calculating the values of k. Since we have selected from columns 2 and 3 and rows 0,1,2,3 we have the matching coordinate sets 
    C=[2,3,2,3,2,3,2,3] and R=[0,0,1,1,2,2,3,3] 
    The number of columns is NC=4. Enter the following statements at the end of your program and run it. Note that we are using n to represent the computed value of the index.

    NC=4
    C=[2,3,2,3,2,3,2,3]
    R=[0,0,1,1,2,2,3,3]
    n=C+R*NC
    PRINT
    PRINT,'C=',C
    PRINT,'R=',R
    PRINT,'k=',k
    PRINT,'n=',n

    The last four lines of the results show the row and column values as well as the old and new values for the array index. This is a check on our calculation.

    C=       2       3       2       3       2       3       2       3
    R=       0       0       1       1       2       2       3       3
    k=       2       3       6       7      10      11      14      15
    n=       2       3       6       7      10      11      14      15
     

  14. Converting between array indexes and (column,row) indexes: We can find the row index from k=C+R*NC by dividing by NC. Because C must be in the range [0,NC-1], we always have C/NC=0 when doing integer division. Therefore, 
    R=k/NC and C=k-R*NC. Verify this by adding the following statements and running prog3. Note that we are using P and Q to represent the computed values of the row and column coordinates.

    P=k/NC

  15. Q=k-P*NC
    PRINT
    PRINT,'k=',k
    PRINT,'R=',R
    PRINT,'P=',P
    PRINT,'C=',C
    PRINT,'Q=',Q

    The last five lines of the output show that the computed value of the rows and columns agree with the known values.

    k=       2       3       6       7      10      11      14      15
    R=       0       0       1       1       2       2       3       3
    P=       0       0       1       1       2       2       3       3
    C=       2       3       2       3       2       3       2       3
    Q=       2       3       2       3       2       3       2       3
     

  16. One can refer to all of the elements of a particular row or column of an array by putting an * in that position. For example, a1[*,2] refers to all of the elements in row 2 because the column position contains a *. Similarly, a1[3,*] refers to all of the elements in column 3. Add the following statements to your program and run it. Does it do what you expect?

    PRINT,a1[*,2]
    PRINT,a1[3,*]

    For the following exercises you should start a new program file to receive your statements. Save the file with a name such as myprog.pro and then run it as you did in the previous exercises.


  17. Construct an integer array A with 6 columns and 3 rows that has each entry equal to its array index and print it out.
  18. Construct an array B in which each element is related to its index by 3*k-10.
  19. Print out the section containing columns 2,3,4 and rows 1,2 of B.
  20. Print out column 3 of A
  21. Print out row 2 of B
  22. Find the index vector k such that PRINT,B[k] will print the values in the following (column,row) positions: (0,2), (1,1), (3,2), (3,0)
  23. Try the following: B[0:2,1:2]=[[2,2,2],[1,1,1]]