Back Home Up


exercices vectors


  1. Create X = INDGEN(100). [NOTE on IDL syntax: INDGEN is a function.  In IDL the arguments of a function are enclosed in parentheses.  In IDL versions 1 through 4, the indices of vector or arra variables were also enclosed in parentheses, as in X(10). Obviously, this introduced possible ambiguities between function and array notation.  Therefore, in IDL version 5, array indices are expected to be enclosed by brackets, as in X[10].  You should use this syntax when referring to array elements.  This notation for array elements is not compatible with IDL V.4 and earlier.  However, IDL V.5 does accept the parenthesis syntax for array elements, so it is compatible with earlier IDL code in this regard.

  2.  Use built-in IDL functions N_ELEMENTS, MIN, MAX, and TOTAL to answer the following:

  3.          How many elements does X contain?  
             What is the minimum value in X?  
             What is the maximum value in X?  
             What is the sum of all the elements in X? 

  4. Use the IDL HyperHelp facility to obtain information on any of the built-in routines you just used, e.g. ?TOTAL.

  5. Print X to your terminal window

  6. How are values in X related to the corresponding subscripts?

  7. Is X a floating-point array?

  8. Print the fifteenth entry in X to your terminal

  9. Then print the entry containing the number 15

  10. Is X a row-vector or a column-vector?

  11. The default configuration of the printed data on your screen will tell you.  To confirm this, try the following:  PRINT,TRANSPOSE(X)

  12. Try the following one-line command and inspect its output:
    FOR I=0,99 DO PRINT,I,X[I]

  13. Now try: FOR I=1,100 DO PRINT,I,X[I]

  14. Using the WHERE function:

  15. Define Q=2*X, then type FIND = WHERE(Q LE 40, COUNT)

  16. Examine the contents of FIND and COUNT so that you understand how the WHERE function operates.

  17. Predict and confirm the response if you type PRINT,Q[FIND]

  18. Print to your terminal the values of the vector X/10

  19. Then print the values of X/10.0 and compare the results.

  20. Do the same for FLOAT(X)/10

  21. Do the same for FIX(X/10.0)

  22. What is the difference between Z = X*0.0 and Z = 0? 

  23. Print the 11 elements centered on X = 10  

  24. First do this using a FOR loop (on a single line)

  25. Then do this using the standard IDL subscript range notation, e.g. X[2:6].  No FOR loop is needed.

  26. Compare to the following: K=5 & PRINT,X[10-K:10+K]

  27. Define Y to be the subarray consisting of those 11 elements, using subscript range notation; print Y to your window as a check.  

  28. Using information utilities: 

  29. Verify lengths of X,Y using the N_ELEMENTS utility

  30. Use the SIZE utility to find the sizes of X and Y; what other info does it supply?

  31. What information does the command HELP,X,Y provide?

  32. Define Q = Y+3 --  Note the values that Q contains

  33. Define Q = Y*3 --  Note the values that Q contains

  34. Define Q = Y^3 --  Note the values that Q contains

  35. Define Q = Y^4 --  Note the values that Q contains; why are they not monotonic?

  36. Define Q = FLOAT(Y)^4 --  Note the change.

  37. Print the vector which results from Q = X*Y

  38. What did IDL actually do to arrive at this result?

  39. Using the built-in functions TOTAL and N_ELEMENTS:

  40. Find and print the mean values of X and Y; 

  41. Find and print the variances of the two arrays

  42. Do the same using the built-in functions MEAN and VARIANCE.

  43. Do the same using the built-in function MOMENT

  44. Determine the nature of the vector which results when you write Q = X & Q[2] = Y.  

  45. What will happen if you write Q[98] = Y?  Try it.

  46. Predict, then verify, the outcome of the following operations: 
        Q = [Y,Y]
        Q = [Q,Y]

  47. Create a 16-element vector [1.01, 1.02, 1.03...] using a simple one-line command employing FINDGEN.  Verify.

  48. Create a 16-element vector: [1,2,4,8,16....] using a simple one-line command employing FINDGEN.  Verify.

  49. Create the 100-element vector Z = 10*X - 0.1*X^2, where X contains the integers between 0 and 99.

  50. Use MAX to determine the maximum value of Z.

  51. Use WHERE to locate the X value for which this maximum occurs.

  52. X was an integer vector but Z is not.  Why?

  53. Using the SHIFT function, shift the elements of the array you just created three entries to the left.  Verify that the maximum is now in the expected location. 

  54. Create, using a simple one-line command employing FINDGEN, a 1001-element vector containing the base-10 logarithms of the integers between 0 and 1000.  Name this "ILOG". 
    Verify its contents. 

  55. Now create a five element vector, Y, containing the numbers 2, 100, 500, 20, and 999.  

  56. Explain the vector resulting when you type NEW = ILOG[Y].  Why did we include an entry for 0 in defining ILOG? 

  57. Optional problem:  ILOG in the previous exercise is called a "lookup table."  It can be used to accelerate computations in problems where a large number of time-consuming transformations such as logarithms are needed.  You can estimate the time savings for this example as follows:

  58. Write a simple one-line IDL command script using "&" as a link between individual commands.  Use the SYSTIME(1) function to determine the start time.  Then, use a FOR loop to compute the logarithm of an integer 40000 times using the standard ALOG10 function.  Then use SYSTIME(1) again to determine the end time.  Print the elapsed time.

  59. Repeat, now using the "ILOG" lookup table instead of ALOG10. 




IDL courses C. Morisset 2004 IA/UNAM V 2.2

Back Home Up