1
$\begingroup$

I want to permute a matrix based on some permutation cycle which is not known beforehand. A function searches through the matrix and returns the pair of indices $i,j$ of the first entry which equals some specific value. Now, I want to permute the matrix such that that column $j$ appears first in the matrix. I would use the function Cycles[{{1,j}}], but it doesn't work if the index $j$ equals 1 (since I want to bring it to the first column).

Is there a way to create a general permutation function such that in the situation that $j=1$, it just returns the identity permutation (so it doesn't permute the matrix)?

$\endgroup$
2
$\begingroup$

You want to exchange column 1 with column j of a matrix. You can use Part to achieves this. Here is an example:

mat = Array[Subscript[a,  #1, #2] &, {3, 3}]
j = 3;
mat[[All, {j, 1}]] = mat[[All, {1, j}]];
mat // MatrixForm

enter image description here

$\endgroup$
3
  • $\begingroup$ Most importantly to the OP is that this works also for j=1. $\endgroup$ – Henrik Schumacher Mar 30 at 10:59
  • $\begingroup$ @ Henrik Schumacher Does it not? $\endgroup$ – Daniel Huber Mar 30 at 11:05
  • $\begingroup$ No, it does. I just meant to point this out once more. =) $\endgroup$ – Henrik Schumacher Mar 30 at 11:13

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.