# How can I construct this matrix?

How can I construct this matrix by MMA?

$$\left(\begin{array}{cccccc}1 & 2 & 3 & \cdots & n-1 & n \\ n & 1 & 2 & \cdots & n-2 & n-1 \\ n-1 & n & 1 & \cdots & n-3 & n-2 \\ \vdots & \vdots & \vdots & & \vdots & \vdots \\ 2 & 3 & 4 & \cdots & n & 1\end{array}\right)$$

• Tenfold challenge: completed ✓
– bmf
Apr 19, 2022 at 22:47
• @bmf It's amazing! The answers are all great. I gave the ✓ to the one with the highest vote. Apr 20, 2022 at 2:17
• Good choice. The accept should be either the answer by Roman or kglr. Quite fun reading through all the alternatives. Nice question!
– bmf
Apr 20, 2022 at 2:21
• Cool question: I wrote it down, wil think about this over the night. Apr 22, 2022 at 13:33

a[n_Integer?Positive] := Array[Mod[#2 - #1, n] + 1 &, {n, n}]

a[6] // MatrixForm


$$\left( \begin{array}{cccccc} 1 & 2 & 3 & 4 & 5 & 6 \\ 6 & 1 & 2 & 3 & 4 & 5 \\ 5 & 6 & 1 & 2 & 3 & 4 \\ 4 & 5 & 6 & 1 & 2 & 3 \\ 3 & 4 & 5 & 6 & 1 & 2 \\ 2 & 3 & 4 & 5 & 6 & 1 \\ \end{array} \right)$$

tm[n_] := ToeplitzMatrix[RotateRight @ Reverse @ Range @ n, Range @ n]

TeXForm @ tm[5]


$$\left( \begin{array}{ccccc} 1 & 2 & 3 & 4 & 5 \\ 5 & 1 & 2 & 3 & 4 \\ 4 & 5 & 1 & 2 & 3 \\ 3 & 4 & 5 & 1 & 2 \\ 2 & 3 & 4 & 5 & 1 \\ \end{array} \right)$$

Here is an approach (pulling out the length as a sort of parameter--set len to whatever you want):

With[
{len=5},
NestList[RotateRight,Range[len],len-1]]


You can use SparseArray and Band

sa[n_] :=
Normal@SparseArray[
Flatten[
{Band[{1, 1}] -> 1,
Table[Band[{1, i}] -> i, {i, 2, n}],
Diagonal[
Table[Table[Band[{k, 1}] -> n - x, {k, 2, n}], {x, 0,
n - 2}]]},
1
],
{n, n}
]

Grid@Partition[MatrixForm /@ Table[sa[xx], {xx, 2, 13}], 3]


Another approach using Fold:

With[{len = 6}, Map[Fold[RotateRight, Array[# - 1 &, len, 2], {#}] &, RotateRight@Range[len]]]


Another approach using Nest:

With[{len = 6}, Map[Nest[RotateRight, Array[# - 1 &, len, 2], #] &, RotateRight@Range[len]]]


Since @kglr used the ToeplitzMatrix, I thought it'd be a good idea to use the HankelMatrix. Well, it was not but I managed to get the following

idm[n_] := IdentityMatrix[n]
hm[n_] := LowerTriangularize[Reverse@HankelMatrix[Range@n] + 1, -1]
diag[n_] :=
Diagonal[Map[Sort,
UpperTriangularize[Transpose@Reverse@HankelMatrix[n]], 1], #] & /@
Range[n - 1]
aux[n_] := Table[diag[n][[xx]] - (xx - 1), {xx, 1, n - 1}]
res[n_] := idm[n] + hm[n] + last[n]


We can check

Grid@Partition[MatrixForm /@ Table[res[i], {i, 2, 13}], 3]


rrmat[n_] := RotateRight[Range[n], #] & /@ Range[0, n - 1]


Test:

MatrixForm /@ (rrmat /@ Range[2, 6])


$$\left\{\left( \begin{array}{cc} 1 & 2 \\ 2 & 1 \\ \end{array} \right),\left( \begin{array}{ccc} 1 & 2 & 3 \\ 3 & 1 & 2 \\ 2 & 3 & 1 \\ \end{array} \right),\left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 4 & 1 & 2 & 3 \\ 3 & 4 & 1 & 2 \\ 2 & 3 & 4 & 1 \\ \end{array} \right),\left( \begin{array}{ccccc} 1 & 2 & 3 & 4 & 5 \\ 5 & 1 & 2 & 3 & 4 \\ 4 & 5 & 1 & 2 & 3 \\ 3 & 4 & 5 & 1 & 2 \\ 2 & 3 & 4 & 5 & 1 \\ \end{array} \right),\left( \begin{array}{cccccc} 1 & 2 & 3 & 4 & 5 & 6 \\ 6 & 1 & 2 & 3 & 4 & 5 \\ 5 & 6 & 1 & 2 & 3 & 4 \\ 4 & 5 & 6 & 1 & 2 & 3 \\ 3 & 4 & 5 & 6 & 1 & 2 \\ 2 & 3 & 4 & 5 & 6 & 1 \\ \end{array} \right)\right\}$$

Another use of FoldList

len = 6;
FoldList[RotateRight, Range[len], ConstantArray[1, len - 1]]

• (+1) and just a related minor comment: @N.J.Evans already used FoldList.
– bmf
Apr 19, 2022 at 19:30
• I think/hope the answer is different enough though... Apr 19, 2022 at 19:31
• totally agree with you on this :-)
– bmf
Apr 19, 2022 at 19:32

Since @bmf challenged us to get to 10:

With[
{len = 5},
MapIndexed[RotateRight[#1, #2 - 1] &, ConstantArray[Range[len], len]]]

• Also a (+1) as the other answer. I think that this 10-fold answers challenge will become a theme for any list and/or array related question. All credits to @Nasser
– bmf
Apr 19, 2022 at 16:27

Is this sufficiently different?

With[
{len=5},
NestList[Mod[#-1,len,1]&,Range[len],len-1]]

• For me it's sufficient
– bmf
Apr 19, 2022 at 18:40

Another silly option:

rotmat[n_] := Module[
{r = Range[n]},
ArrayReshape[
FoldList[{r[[-#2[[1]] ;;]], r[[;; -#2[[2]]]]} &, r,
Partition[r, 2, 1]], {n, n}]
]

• Nicely done and we are one step closer :-)
– bmf
Apr 19, 2022 at 19:15
n = 5;

Table[RotateRight[Range[n], i], {i, 0, n - 1}] // MatrixForm


n = 2;

Table[RotateRight[Range[n], i], {i, 0, n - 1}] // MatrixForm