11
$\begingroup$

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)$

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

11 Answers 11

21
$\begingroup$
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) $$

$\endgroup$
16
$\begingroup$
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)$$

$\endgroup$
13
$\begingroup$

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]]
$\endgroup$
8
$\begingroup$

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]

res

$\endgroup$
7
$\begingroup$
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\}$$

$\endgroup$
7
$\begingroup$

Another use of FoldList

len = 6; 
FoldList[RotateRight, Range[len], ConstantArray[1, len - 1]] 
$\endgroup$
3
  • $\begingroup$ (+1) and just a related minor comment: @N.J.Evans already used FoldList. $\endgroup$
    – bmf
    Apr 19 at 19:30
  • $\begingroup$ I think/hope the answer is different enough though... $\endgroup$
    – bill s
    Apr 19 at 19:31
  • $\begingroup$ totally agree with you on this :-) $\endgroup$
    – bmf
    Apr 19 at 19:32
7
$\begingroup$

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]]]

enter image description here

$\endgroup$
6
$\begingroup$

Since @bmf challenged us to get to 10:

With[
  {len = 5},
  MapIndexed[RotateRight[#1, #2 - 1] &, ConstantArray[Range[len], len]]]
$\endgroup$
1
  • 1
    $\begingroup$ 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 $\endgroup$
    – bmf
    Apr 19 at 16:27
6
$\begingroup$

Is this sufficiently different?

With[
  {len=5},
  NestList[Mod[#-1,len,1]&,Range[len],len-1]]
$\endgroup$
1
  • $\begingroup$ For me it's sufficient $\endgroup$
    – bmf
    Apr 19 at 18:40
6
$\begingroup$

Another silly option:

rotmat[n_] := Module[
  {r = Range[n]},
  ArrayReshape[
   FoldList[{r[[-#2[[1]] ;;]], r[[;; -#2[[2]]]]} &, r, 
    Partition[r, 2, 1]], {n, n}]
  ]
$\endgroup$
1
  • $\begingroup$ Nicely done and we are one step closer :-) $\endgroup$
    – bmf
    Apr 19 at 19:15
6
$\begingroup$

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}]
last[n_] := PadRight[ArrayPad[PadLeft[aux[n]], {0, {1, 0}}], {n, n}]
res[n_] := idm[n] + hm[n] + last[n]

We can check

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

matgrid

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.