[SciPy-user] manipulate arrays/matrices

Nils Wagner nwagner at iam.uni-stuttgart.de
Wed Jun 7 02:42:19 CDT 2006


Hi all,

Is there a better way to interchange two rows/columns of an array ?

from scipy import *

n=6

A = diag(arange(1,n+1))+diag(ones(n-2),2)+diag(ones(n-2),-2)

#
# Columns/Rows
#
j = 1
k = 2

print
print 'A_old'
print
print A
#
# Row interchange
#
tmp = A[j,:].copy()
A[j,:] = A[k,:]
A[k,:] = tmp
#
# Column interchange
#
tmp = A[:,j].copy()
A[:,j] = A[:,k]
A[:,k] = tmp

print
print 'A_new'
print
print A



Nils




More information about the SciPy-user mailing list