MATLAB: Rearrange order of a diagonal interaction matrix

MATLABmatrixrearrangement

Dear MATLAB users,
I have an interaction matrix, lets say I have 3 elements "a", "b", "c". The interaction matrix is A.
so since it is an interaction matrix, interaction energy of "ab" is equal to "ba" making the matrix diagonal. I know the values of every interaction.
Now I want to change the order of the interaction matrix from a,b,c to b,c,a(or any other order!)
how can I convert the matrix into the new one. I would highly appreciate your kind attention.
you may create a 3×3 random matrix like this:
% Constructing the interaction matrix
for i=1:3
for j=1:3
if i>=j
A(i,j)=rand();
end
end
end
for i=1:3
for j=1:3
A(i,j)=A(j,i)
end
end
so lets say that the interaction energy matrix is:
A =
a b c
a 0.2185 0.8995 0.8727
b 0.8995 0.9230 0.9742
c 0.8727 0.9742 0.5763
where A(1,1) is the interaction of "aa" and A(2,3) is the interaction energy of "bc" which is equal to A(3,2) that is interaction energy of "cb".
now if I want to construct this matrix and change the order to b, c, a what will be the A matrix?
A' =
b c a
b 0.9230 0.9742 0.8995
c 0.9742 0.5763 0.8727
a 0.8995 0.8727 0.2185
This was just an example, I have a 20X20 matrix that I want to change order of the alphabets.
Best
Argu

Best Answer

Using basic subscript indexing:
>> A = [0.2185,0.8995,0.8727;0.8995,0.9230,0.9742;0.8727,0.9742,0.5763]
A =
0.21850 0.89950 0.87270
0.89950 0.92300 0.97420
0.87270 0.97420 0.57630
>> X = [2,3,1];
>> B = A(X,X)
B =
0.92300 0.97420 0.89950
0.97420 0.57630 0.87270
0.89950 0.87270 0.21850
Your example shows the diagonal sorted into descending order, for which X is easy to define:
[~,X] = sort(-diag(A))
Related Question