### Ansys Matrix Sorting

Sorting*

The *MOPER command has a sort which can be quite convenient. Here's an example of sorting the matrix of different columns consecutively.

/PREP7
! Creates Matrix
*dim, mymatrix, array, 5, 3
mymatrix(1, 1) = 4
mymatrix(2, 1) = 7
mymatrix(3, 1) = 4
mymatrix(4, 1) = 1
mymatrix(5, 1) = 4

mymatrix(1, 2) = 42
mymatrix(2, 2) = 78
mymatrix(3, 2) = 42
mymatrix(4, 2) = 16
mymatrix(5, 2) = 22

mymatrix(1, 3) = 3
mymatrix(2, 3) = 8
mymatrix(3, 3) = 2
mymatrix(4, 3) = 6
mymatrix(5, 3) = 5

! Writes Original Matrix
*cfopen, origmatrix, txt
*vwrite, mymatrix(1,1), mymatrix(1,2), mymatrix(1,3)
(E20.5, ' ', E20.5, ' ', E20.5)
*cfclos

The original "mymatrix" is:
4 42 3
7 78 8
4 42 2
1 16 6
4 22 5

! Reorder Matrix by first column, then second, then third
*dim, ordering,,5 ! Ordering index array
*moper, ordering(1), mymatrix(1,1), sort, , 1,2,3 ! ordering column

! Writes Sorted Matrix
*cfopen, newmatrix, txt
*vwrite, mymatrix(1,1), mymatrix(1,2), mymatrix(1,3)
(E20.5, ' ', E20.5, ' ', E20.5)
*cfclos

The new "mymatrix" that is sorted is now:
1 16 6
4 22 5
4 42 2
4 42 3
7 78 8

The ordering array contains the initial row position which could be useful if the matrix is to be put back together. "ordering":
4
5
3
1
2

Note that *VWRITE cannot be run within the GUI but must be called from a file using /INPUT or *USE. The Ansys script can be found here [Link].