3
$\begingroup$

I have two 2-D matrices $A$ and $B$(owns same dimentions) which have the style written as below:

$ \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1k} &\cdots &\cdots & a_{1,2k+1}\\ a_{21} & a_{22} & \cdots & a_{2k} &\cdots &\cdots & a_{2,2k+1}\\ \vdots & \vdots & \vdots & \vdots &\vdots &\cdots & \vdots \\ a_{k1} & a_{k2} & \cdots & a_{kk} &\cdots &\cdots & a_{k,2k+1}\\ a_{k+1,1} & a_{k+1,2} & \cdots & a_{k+1,k+1} & a_{k+1,k+1} &\cdots a_{k+1,2k} & a_{k+1,,2k+1}\\ a_{k+2,1} & a_{k+2,2} & \cdots & a_{k+2,k} &\cdots &\cdots & a_{k+2,,2k+1}\\ \vdots & \vdots & \vdots & \vdots &\vdots & \vdots \\ a_{2k+1,1} & a_{2k+1,2} & \cdots & a_{2k+1,k} &\cdots &\cdots & a_{2k+1,2k+1}\\ \end{bmatrix} _{2k+1\times 2k+1} $

Now I want to swap the value of some positions betwen them

enter image description here

My awkward solution

Edit Thanks for @Kuba

swap[a_, b_] :=
 Module[{k, A = a, B = b},
  k = Quotient[Length@A, 2];

  {A[[1 ;; k, 1 ;; k]], B[[1 ;; k, 1 ;; k]]} =
   {B[[1 ;; k, 1 ;; k]], A[[1 ;; k, 1 ;; k]]};

  {A[[k + 2 ;; 2 k + 1, 1 ;; k]], B[[k + 2 ;; 2 k + 1, 1 ;; k]]} =
   {B[[k + 2 ;; 2 k + 1, 1 ;; k]], A[[k + 2 ;; 2 k + 1, 1 ;; k]]};

  {A[[k + 1, k + 1 ;; 2 k]], B[[k + 1, k + 1 ;; 2 k]]} =
   {B[[k + 1, k + 1 ;; 2 k]], A[[k + 1, k + 1 ;; 2 k]]};
  {A, B}
]

Test

mat1 = Partition[Range[1, 25], 5];
mat2 = Partition[Range[26, 50], 5];
MatrixForm /@ {mat1, mat2}
MatrixForm /@ swap[mat1, mat2]

enter image description here

In addition, I have a trial to refactor the code

  {A[[1 ;; k, 1 ;; k]], B[[1 ;; k, 1 ;; k]]} =
   {B[[1 ;; k, 1 ;; k]], A[[1 ;; k, 1 ;; k]]};

  {A[[k + 2 ;; 2 k + 1, 1 ;; k]], B[[k + 2 ;; 2 k + 1, 1 ;; k]]} =
   {B[[k + 2 ;; 2 k + 1, 1 ;; k]], A[[k + 2 ;; 2 k + 1, 1 ;; k]]};

to the below style

{A[[{1 ;; k, k + 2 ;; 2 k + 1}, 1 ;; k]], B[[{1 ;; k, k + 2 ;; 2 k + 1}, 1 ;; k]]} =
 {B[[{1 ;; k, k + 2 ;; 2 k + 1}, 1 ;; k]], A[[{1 ;; k, k + 2 ;; 2 k + 1}, 1 ;; k]]}

However, I failed in this trial and it seems that Span(;;) doesn't own this usage.

Question

  • Is there other better methods(solutions) to implement this swap operation?
$\endgroup$
  • $\begingroup$ There are two $a_{k+1,k+1}$ in the matrix? $\endgroup$ – xzczd Apr 23 '15 at 10:14
  • $\begingroup$ @xzczd, a mistake:D $\endgroup$ – xyz Apr 23 '15 at 14:00
4
$\begingroup$
ClearAll[swapF]
swapF = Module[{m1 = #, m2 = #2, k = Floor[Length[#]/2]},
    Module[{copy = #, rows = Join @@ {Range[k], Range[k + 2, 2 k + 1]}},
       CompoundExpression[copy[[rows, ;; k]] = #2[[rows, ;; k]], 
           copy[[k + 1, k + 1 ;;2k]] = #2[[k + 1, k + 1 ;;2k]]]; copy] & @@@ 
      {{m1, m2}, {m2, m1}}] &;

Example:

mat1 = Partition[Range[1, 25], 5];
mat2 = Partition[Range[26, 50], 5];
Column@(Row /@ {MatrixForm /@ {mat1, mat2}, MatrixForm /@ swapF[mat1, mat2]}) 

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.