[SciPy-user] manipulate arrays/matrices

Pau Gargallo pau.gargallo at gmail.com
Wed Jun 7 04:03:15 CDT 2006


On 6/7/06, Nils Wagner <nwagner at iam.uni-stuttgart.de> wrote:
> 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
>

your code looks very good to me.

i just noticed that the 'python way':

A[j], A[k] = A[k], A[j]

only works with 1d arrays.
Is this wanted?

pau

(this is probably a numpy-discussion question)



More information about the SciPy-user mailing list