Coyote's Guide to IDL Programming

Reversing the Even Rows of a 2D Array

QUESTION: Jouhahn Lee (jl@maxwell.ph.kcl.ac.uk) at King's College London writes:

I have a 64-by-32 data array. In one of my processing steps I need to reverse the data in just the even rows. The data in the odd rows should remain the same. Can someone show me a simple way to do this in IDL?

ANSWER: The trick here is to create an index into the even rows of the data array. Once you have the index, the IDL Reverse function reverses the order of the data. The solution looks like this:

   data = RandomU(seed, 64, 32)
   evenRowIndex = (Indgen(32/2) * 2) + 1
   data[*, evenRowIndex] = Reverse(data[*, evenRowIndex])

Try it on a smaller data set:

   IDL> data = Findgen(8,4)
   IDL> Print, data
      0.000000  1.00000  2.00000  3.00000  4.00000  5.00000  6.00000  7.00000
      8.00000   9.00000  10.0000  11.0000  12.0000  13.0000  14.0000  15.0000
      16.0000   17.0000  18.0000  19.0000  20.0000  21.0000  22.0000  23.0000
      24.0000   25.0000  26.0000  27.0000  28.0000  29.0000  30.0000  31.0000
   IDL> evenRowIndex = (Indgen(4/2) * 2) + 1
   IDL> data[*, evenRowIndex] = Reverse(data[*, evenRowIndex])
   IDL> Print, data
      0.000000  1.00000  2.00000  3.00000  4.00000  5.00000  6.00000  7.00000
      15.0000   14.0000  13.0000  12.0000  11.0000  10.0000  9.00000  8.00000
      16.0000   17.0000  18.0000  19.0000  20.0000  21.0000  22.0000  23.0000
      31.0000   30.0000  29.0000  28.0000  27.0000  26.0000  25.0000  24.0000

Google
 
Web Coyote's Guide to IDL Programming