RE: basic operations in matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg34608] RE: [mg34542] basic operations in matrices
- From: "DrBob" <majort at cox-internet.com>
- Date: Wed, 29 May 2002 02:46:43 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
Guten Tag Christian, Here's a slightly better solution that may be worth looking at. Remove["Global`x*"] dim = 5; rows = ToString[Unique["x"]] & /@ Range[dim]; m = Transpose[(Unique /@ rows) & /@ Range[dim]]; m // MatrixForm m[[All, {1, 3, 2, 4, 5}]] // MatrixForm m[[{1, 3, 2, 4, 5}, All]] // MatrixForm << DiscreteMath`Combinatorica` ClearAll[pair] pair[i_, j_, n_] := FromCycles[Join[List /@ Complement[Range[n], {i, j}], {{i, j}} ] ] rowSwitch[m_, i_, j_] := m[[pair[i, j, Length[m]], All]] columnSwitch[m_, i_, j_] := m[[All, pair[i, j, Length[m]]]] rowSwitch[m, 1, 3] // MatrixForm columnSwitch[m, 1, 3] // MatrixForm rowSwitch[columnSwitch[m, 1, 3], 2, 4] // MatrixForm Bobby Treat -----Original Message----- From: Christian Junk [mailto:junkc at fh-trier.de] To: mathgroup at smc.vnet.net Subject: [mg34608] Re: [mg34542] basic operations in matrices Guten Tag DrBob, am Montag, 27. Mai 2002 um 09:52 schrieben Sie: D> Christian, D> In the following, I've generated a matrix of symbols (m). Then I show m D> first with the second and third columns switched, then with the second D> and third rows switched. D> Remove["Global`*"] D> letters = {"a", "b", "c", "d"}; D> m = (Unique /@ letters) & /@ Range[4]; D> m // MatrixForm D> m[[All, {1, 3, 2, 4}]] // MatrixForm D> m[[{1, 3, 2, 4}, All]] // MatrixForm D> There are other ways, but I think this is the easiest. D> Here are functions to switch any two rows or columns. (This may not be D> the easiest way, but it's an interesting way!) D> << DiscreteMath`Combinatorica` D> pair[i_, j_, n_] := D> FromCycles[Join[{{i, j}}, Complement[ToCycles[Range[n]], {#} & /@ {i, D> j}] D> ] D> ] D> rowSwitch[m_, i_, j_] := m[[pair[i, j, Length[m]], All]] D> columnSwitch[m_, i_, j_] := m[[All, pair[i, j, Length[m]]]] D> rowSwitch[m, 1, 3] // MatrixForm D> columnSwitch[m, 1, 3] // MatrixForm D> rowSwitch[columnSwitch[m, 1, 3], 2, 4] // MatrixForm D> Bobby Treat D> -----Original Message----- D> From: Christian Junk [mailto:junkc at fh-trier.de] To: mathgroup at smc.vnet.net D> Sent: Monday, May 27, 2002 12:16 AM D> Subject: [mg34608] [mg34542] basic operations in matrices D> Hi! D> I'm new to Mathematica and I'm searching for an operation, that simply D> transpose two rows or columns of a matrix M. D> e.g: D> Operation[M,{1,3}] D> 1 1 1 1 3 3 3 3 2 2 2 2 --->> 2 2 2 2 D> 3 3 3 3 1 1 1 1 D> 4 4 4 4 4 4 4 4 D> Much thanks in advance, D> Christian Hi! Thank you for your answer. It helped me a lot solving my problems with Mathematica! -- Christian Junk mailto:junkc at fh-trier.de