Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
February 20th, 2005, 04:26 PM
 fupduck
Registered User

Join Date: Feb 2005
Posts: 4
Time spent in forums: 1 h 16 m 38 sec
Reputation Power: 0
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
February 20th, 2005, 07:37 PM
 sfb
Contributing User

Join Date: Nov 2003
Posts: 624
Time spent in forums: 3 Days 6 h 10 m 21 sec
Reputation 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).

#3
February 20th, 2005, 07:42 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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

#4
February 20th, 2005, 08:01 PM
 sfb
Contributing User

Join Date: Nov 2003
Posts: 624
Time spent in forums: 3 Days 6 h 10 m 21 sec
Reputation 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]]```

#5
February 20th, 2005, 08:29 PM
 fupduck
Registered User

Join Date: Feb 2005
Posts: 4
Time spent in forums: 1 h 16 m 38 sec
Reputation 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

#6
February 21st, 2005, 03:34 AM
 DevCoach
Contributing User

Join Date: Feb 2004
Location: London, England
Posts: 1,585
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
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)).

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Array manipulation

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |