Rearrange a matrix with new Table Headings-List

I have used a procedure to rearrange my data in a more appropriate way for data analysis such that more similar elements lie next to each other and dissimilar ones far apart.

Let's say I have a 5x5 matrix representing the correlation of 5 assets with the headings x = {a,b,c,d,e}. I used an algorithm which gives me an output y = {c,a,e,b,d}, the new order which fulfills my first criterion.

Could I somehow use the new structure to reorder the old matrix by the new seriated labels?

Ploting the old one would look like TableForm[Corr, TableHeadings -> {x, x}] (Corr being the matrix), and I want the new one to be: TableForm[CorrNew, TableHeadings-> {y,y}] but obviously with its proper matrix elements.

Try this. To be precise, let us assume that the following

matr={{7, 10, -2, 3, -4}, {-8, -8, -9, 3, 3}, {5, -1, 7, 9, -5}, {4, -9, 0, -7, -10}, {10, 5, -9, 8, -9}};

is your matrix to be rearranged.

lst = {a, b, c, d, e};
lst2 = {c, a, e, b, d};

Let us determine positions of the elements of lst2 in the lst:

newList = Position[lst, #] & /@ lst2 // Flatten

(*  {3, 1, 5, 2, 4}  *)

tab=Join[{lst}, matr];

It is convenient to look at it in the MatrixForm

MatrixForm[tab2] Let us now transpose it and call the transposed table tab2:

tab2 = Transpose[tab]

(*  {{a, 7, -8, 5, 4, 10}, {b, 10, -8, -1, -9, 5}, {c, -2, -9, 7,
0, -9}, {d, 3, 3, 9, -7, 8}, {e, -4, 3, -5, -10, -9}}  *)

and make a list of replacements:

repl = Table[Rule[i, tab2[[newList[[i]]]]], {i, 1, 5}]

(* {1 -> {c, -4, -7, -7, 8, 2}, 2 -> {a, 8, -1, -9, -6, -9},
3 -> {e, 9, 0, -1, 9, 4}, 4 -> {b, -8, -7, 3, -4, 7},
5 -> {d, 5, -7, 8, -7, -4}} *)

Now we can form the new table. Let us call it tab3:

tab3 = Transpose@ReplacePart[tab2, repl]

(*  {{c, a, e, b, d}, {-4, 8, 9, -8, 5}, {-7, -1, 0, -7, -7}, {-7, -9, -1,
3, 8}, {8, -6, 9, -4, -7}, {2, -9, 4, 7, -4}}  *)

Again, it is convenient to look at it in the matrixForm:

MatrixForm[tab3] Have fun!

• Thank you very much as well! That has helped me alot!
– Axha
Aug 20 '21 at 14:41

Update

With m, order, and neworder from below then

ResourceFunction["SymmetricSort"][m, order, neworder] OP

When you reorder the correlation matrix you must preserve the diagonal and the relationship between the variables.

For correlation matrix m

m = Transpose@MapIndexed[
{v, i} |-> Indexed[v, Flatten@{#, i}] & /@ Range@4
, {a, b, c, d}
];
MatrixForm@m Need the $$(a_{11},b_{22},c_{33},d_{44})$$ values on the diagonal and remain a symmetric positive semidefinite matrix. Therefore both the columns and the rows need to be reordered.

With

order = {a, b, c, d};
neworder = {b, c, a, d};

Then

newm =
With[
{s = neworder /. Thread[order -> Range@Length@order]}
, m[[s, s]]
];
MatrixForm@newm The diagonal and positioning of correlations between variables is preserved. Matrix remains a symmetric positive semidefinite matrix.

Hope this helps.

• Thank you very much! That has helped me alot!
– Axha
Aug 20 '21 at 14:40