# Using MinimumBandwidthOrdering to restructure a correlation matrix

I am trying to restructure a correlation matrix such that the variables with the strongest correlations are grouped together

Needs["GraphUtilities"]


These are the column headers of the data'

t = {"Age", "Blood Pressure", "Specific Gravity", "Albumin", "Sugar","Red Blood Cells"}


This is the data

m={{1.,0.131701,-0.107092,0.0781839,0.188485,-0.00275326},{0.131701,1.,-0.123132,0.122221,0.189071,-0.149835},{-0.107092,-0.123132,1.,-0.502224,-0.291435,0.269056},{0.0781839,0.122221,-0.502224,1.,0.286883,-0.394184},{0.188485,0.189071,-0.291435,0.286883,1.,-0.0923074},{-0.00275326,-0.149835,0.269056,-0.394184,-0.0923074,1.}}


These are the ticks for the matrix plot

ticks = {{Table[{i, t[[i]]}, {i, 1, Length@t}], None}, {None,
Table[{i, Rotate[columnHeaders[[i]], 90 Degree]}, {i, 1,
Length@t}]}};


and here is the correlation matrix

matplot =MatrixPlot[m, Mesh -> True,ColorFunction -> ColorData[{"TemperatureMap", "Reverse"}],FrameTicks -> ticks]


I am attempting to use MinimumBandwidthOrdering[] to essentially restructure the matrix so that the strongest correlated varibales are grouped together.

{r, c} = MinimumBandwidthOrdering[m]


Plotting the matrices side by side

{MatrixPlot[m], MatrixPlot[m[[r, c]]]}


My problem is this: How can i plot the the matrices such that when the matrix is transformed, the column headers that are associated with that variable are transformed with it.

from the plots, it is just labelled as: 1,2,3,4,5,6 Whereas i was hoping it to be in the format of the plot that was given from the matplot function.

• With t[[c]]...? Commented Mar 5, 2020 at 13:46
• If you mean t[[c]] as in the collum headers appended to the collums yes. But with t appended to the columns and rows as shown in the plot from the matplot[] correlation matrix. Commented Mar 5, 2020 at 13:50
• use ticks2 = {{Table[{i, t[[r]][[i]]}, {i, 1, Length@t}], None}, {None, Table[{i, Rotate[t[[c]][[i]], 90 Degree]}, {i, 1, Length@t}]}}; for FrameTicks in MatrixPlot of m[[r,c]]?
– kglr
Commented Mar 5, 2020 at 14:39

Define ticks2 using t[[r]] and t[[c]] in place of t in your code for ticks.
ticks2 = {{Table[{i, t[[r]][[i]]}, {i, 1, Length@t}], None}, {None,
`