#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    4
    Rep Power
    0

    Question Array manipulation


    I have a 2D array similar to the following:
    A = [[ 1 2 3 4]
    [ 5 6 7 8]
    [ 9 10 11 12]
    [13 14 15 16]]

    I need to swap quadrants 1 and 3 and then 2 and 4 to get some like:

    A = [[ 11 12 9 10]
    [ 15 16 13 14]
    [ 3 4 1 2]
    [ 7 8 5 6]]

    Any help is greatly appreciated.

    f
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    This should do it:

    Code:
    >>> a = [[1,2,3,4],
    ... [5,6,7,8],
    ... [9,10,11,12],
    ... [13,14,15,16]]
    >>>
    >>> r1, r2, r3, r4 = a
    >>> a = [r3[2:]+r3[:2], r4[2:]+r4[:2], r1[2:]+r1[:2], r2[2:]+r2[:2]]
    >>> 
    >>> a
    [[11, 12, 9, 10], [15, 16, 13, 14], [3, 4, 1, 2], [7, 8, 5, 6]]
    >>>
    But it's a bit ugly. It takes each row out, then builds a new list by taking the parts of each row needed.

    The new first row being: r3[2:]+r3[:2]
    (the third row after the second character followed by the third row up to the second character).
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The simplest way I can think of. You can also do it using several temp variables to hold each element and put them into the list in their new position. Although I think this is a little more elegant .

    Code:
    >>> target = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
    >>> target
    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
    >>> 
    >>> for each in range(2): target.append(target.pop(0))
    ... 
    >>> target
    [[9, 10, 11, 12], [13, 14, 15, 16], [1, 2, 3, 4], [5, 6, 7, 8]]
    >>>
    >>> for eachList in target:
    ...     for index in range(2):
    ...         eachList.append(eachList.pop(0))
    ... 
    >>> target
    [[11, 12, 9, 10], [15, 16, 13, 14], [3, 4, 1, 2], [7, 8, 5, 6]]
    >>>
    Enjoy,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Code:
    >>> rows = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]
    >>> 
    >>> for row in rows:
    ... 	row[:] = [row[2], row[3], row[0], row[1]]
    ... 	
    >>> rows = [rows[2], rows[3], rows[0], rows[1]]
    >>> rows
    [[11, 12, 9, 10], [15, 16, 13, 14], [3, 4, 1, 2], [7, 8, 5, 6]]
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    4
    Rep Power
    0
    Thanks for the help, but I left out an important point. I need to eventually use the code on any 2D matrix. I have Matlab code that does this but haven't been able to convert it Python.

    Matlab code:

    for jj=1:ny
    for ii=1:kx-1
    data(ii+kx+1,jj)=dat(ii,jj);
    end
    for ii=kx:nx
    data(ii-kx+1,jj)=dat(ii,jj);
    end
    end

    where,
    ny = number columns
    kx = nx/2

    This swaps quadrant 1 and 3. The code for 2 and 4 is basically the same. Again, thanks for the help.

    p.s. The end result is correctly calculate the power spectrum using Fourier transform. Matlab has a function fftshift() but I haven't seen anything within any Python module.

    f
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Have you looked at the numpy or numarray libraries? Both of these are high performance C extensions for manipulating arrays.

    You can extend the python code in the previous posts to handle N*N arrays [1], but any python implementation will be probably be far too slow for a useful FFT algorithm. Both numpy and numarray are orders of magnitude faster.

    Dave - The Developers' Coach

    [1] e.g. in netytan's post, replace both the range(2) with range(int(N/2)).

IMN logo majestic logo threadwatch logo seochat tools logo