Skip to main content
added 7 characters in body
Source Link
xyz
  • 655
  • 4
  • 38
  • 117
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

enter image description here

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 ;;]] = #2[[k + 1, k + 1 ;;]]]; 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

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

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

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 ;;]] = #2[[k + 1, k + 1 ;;]]]; 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