Switching the position of matrix elements in Mathematica

AI Thread Summary
To switch the positions of specific elements in a matrix in Mathematica, one effective method involves using nested loops to iterate through the matrix. By targeting the indices of the elements to be swapped, such as a12 with a21 and a14 with a23, the positions can be exchanged directly. The provided code snippet demonstrates this approach, utilizing a For loop to swap elements in pairs. Alternatively, breaking the matrix into smaller 2x2 submatrices, transposing them, and then reassembling the full matrix is another viable technique. Both methods achieve the desired rearrangement of matrix elements efficiently.
Qubix
Messages
82
Reaction score
1
Say I have the following matrix, written in Mathematica

A := {{a11, a12, a13, a14}, {a21, a22, a23, a24}, {a31, a32, a33,
a34}, {a41, a42, a43, a44}}

I want to exchange the positions of a12 and a21, a14 and a23, a32 and a41 , a34 and a43.

Any ideas?

Thanks.
 
Physics news on Phys.org
Notice it you broke A into four 2x2 matricies, did a Transpose of each submatrix and reassembled the result back into a full matrix you would have what you want. But I can't think of a cute trick to split the matrix apart.

Either that or just brute force it
Code:
In[1]:= A={
   {a11, a12, a13, a14},
   {a21, a22, a23, a24},
   {a31, a32, a33, a34},
   {a41, a42, a43, a44}};
For[r=1, r<=3, r+=2,
  For[c=1, c<=3, c+=2,
   {A[[r+1, c]], A[[r, c+1]]} = {A[[r, c+1]], A[[r+1, c]]}
   ]
  ];
A

Out[3]= {
 {a11, a21, a13, a23},
 {a12, a22, a14, a24},
 {a31, a41, a33, a43},
 {a32, a42, a34, a44}}
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Back
Top