I would like to swap rows with other rows or columns. Not for any particular size but for any size. I saw this detailed Q&A but it didn't answer my query.
I want to swap multiple rows or columns for a $N\times N$ (variable) matrix. I tried with lots of While
and If
loops but with no success.
How to write a program in Mathematica for this?
As an example, I am interested in swapping rows for an even $N$ as follows:
- row 1 and $N$ are fixed,
- row 2 becomes row 3, row 3 becomes row 2,
- row 4 becomes row 5, row 5 becomes row 4,
- and so on...
In Fortran this is trivial but how to implement this in Mathematica?
program swap // Fortran version
integer Ndim,i
Ndim=40
do, i=1:Ndim
do, j=1:Ndim
If(i<j)then
matrix(i,j)=i+j
endif
enddo
enddo
Ndim=20
do i=1,Ndim
if (mod(j,2)==0 .and. j < Ndim) then
matrix(i,j)=matrix(i,j+1)
elseif(mod(j,2)!=0 .and. j<Ndim)then // != not equal to
matrix(i,j)=matrix(i,j-1)
endif
end do
end program
Any starting point will be very valuable. Thanks a lot for any help.