MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: basic operations in matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34592] Re: basic operations in matrices
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 29 May 2002 02:44:52 -0400 (EDT)
  • References: <acsgc6$kbr$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Christian,

    m= {{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}};

If, as here, your matrix already has a name then:

    m[[{3,2,1,4}]]

        {{3,3,3,3},{2,2,2,2},{1,1,1,1},{4,4,4,4}}

A generally applicable function is

    SwapRows[i_,j_][m_]:=
      Block[{t=m},
        t[[{i,j}]]= m[[{j,i}]];t
        ]

    SwapRows[1,3][m]

        {{3,3,3,3},{2,2,2,2},{1,1,1,1},{4,4,4,4}}

For columns we have, making a new m to show this

    m= Transpose[m]

        {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}}

    m[[All, {3,2,1,4}]]

        {{3,2,1,4},{3,2,1,4},{3,2,1,4},{3,2,1,4}}

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Christian Junk" <junkc at fh-trier.de> wrote in message
news:acsgc6$kbr$1 at smc.vnet.net...
> Hi!
>
> I'm new to Mathematica and I'm searching for an operation, that simply
> transpose two rows or columns of a matrix M.
>
> e.g:
>
> Operation[M,{1,3}]
>
> 1 1 1 1             3 3 3 3
> 2 2 2 2   --->     2 2 2 2
> 3 3 3 3             1 1 1 1
> 4 4 4 4             4 4 4 4
>
> Much thanks in advance,
>
> Christian
>






  • Prev by Date: Re: A question about Mathematica
  • Next by Date: How write or Export a Cell holding a Matrix in //MatrixForm format to an external File
  • Previous by thread: Re: basic operations in matrices
  • Next by thread: RE: basic operations in matrices