# How does one transpose in place the off-diagonal 16 x 16 blocks of a 32 x 32 matrix?

I have a 32 x 32 matrix U. I want to create a new matrix (the "partial transpose"), leaving in place the two 16 x 16 diagonal blocks of U, and transposing in place the two 16 x 16 off-diagonal blocks. How might/should I do this?

Also, I create U itself from a 4 x 4 array of 8 x 8 matrices, call them V[[i,j]], by using

ArrayReshape[{{V[[1,1]],V[[1,2]],V[[1,3]],V[[1,4]]},
{V[[2,1]],V[[2,2]],V[[2,3]],V[[2,4]]},
{V[[3,1]],V[[3,2]],V[[3,3]],V[[3,4]]},
{V[[4,1]],V[[4,2]],V[[4,3]],V[[4,4]]}},{32,32}]


This I take it should work in the "natural" fashion. Could the same be accomplished with the Flatten command? Would some other code be appropriate?

• And V is defined as...? – Feyre Nov 1 '16 at 22:39
• Feyre asked how V is defined. While I think the question is essentially independent of the precise nature of V, I will say that the 8 x 8 matrices are real representations of octonions (eq. (1) in actaphys.uj.edu.pl/fulltext?series=Reg&vol=47&page=1113) the values of which I set to normal distribution (Ginibre ensemble) values. – Paul B. Slater Nov 2 '16 at 2:25

Consider

n = 8;
(U = RandomInteger[{0, 9}, {n, n}]) // MatrixForm


(T = Partition[U, {n/2, n/2}]) // MatrixForm


(t = Transpose @ T) // MatrixForm


ArrayFlatten @ t // MatrixForm


In the above I interpreted "transposing in place the two 16 x 16 off-diagonal blocks" as interchanging the position of the two off-diagonal blocks.

The OP clarified in a comment that he wants to transpose the two blocks themselves, so for this matter:

t = T;

t[[1, 2]] = Transpose @ t[[1, 2]];
t[[2, 1]] = Transpose @ t[[2, 1]];

ArrayFlatten @ t // MatrixForm


or compactly, as C.E. proposed:

t = ArrayFlatten @ MapAt[Transpose, T, {{1,2},{2,1}}]

• +1, this is a nicely written answer. If "transposing in place the two 16 x 16 off-diagonal blocks" means transposing each block individually, I'd recommend ArrayFlatten@MapAt[Transpose, T, {{1,2},{2,1}}]. – C. E. Nov 1 '16 at 22:57
• What the code of corey979 accomplishes, I think, is to interchange the two off-diagonal blocks, while what I intended was not to interchange them, but just to transpose them in place (emphasis added). The two approaches would be equivalent in terms of further analyses if they were to yield the same set of eigenvalues. (The 32 x 32 matrix is symmetric, by prior construction--which I had not mentioned--so the eigenvalues would then be real.) The answer of C. E. appears to accomplish what I intended--so I will mark it as an answer, upon a little further reflection/checking. Thanks to all! – Paul B. Slater Nov 2 '16 at 0:07
• Well, while the comment of C. E. does answer my (first/main) question, it is provided as a comment not an answer--so I can not directly vote for it. – Paul B. Slater Nov 2 '16 at 2:35
• @PaulB.Slater Please see the edited answer. – corey979 Nov 2 '16 at 9:51