Skip to main content
added 186 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
mtrx // MatrixForm

enter image description here

Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

Update 2: shorter version:

rttF2 = Function[{mat}, With[{m = Last@Dimensions[mat]},
   SparseArray[Band[{m + 1 - #, #}] -> Transpose[mat][[#]] & /@ Range[m]]]]
array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
mtrx // MatrixForm

enter image description here

Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
mtrx // MatrixForm

enter image description here

Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

Update 2: shorter version:

rttF2 = Function[{mat}, With[{m = Last@Dimensions[mat]},
   SparseArray[Band[{m + 1 - #, #}] -> Transpose[mat][[#]] & /@ Range[m]]]]
added 150 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
mtrx // MatrixForm

enter image description here

Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
mtrx // MatrixForm

enter image description here

Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

added 624 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

array0 = {{72, 32, 64}, {18, 8, 16}, {63, 28, 56}}; 
array1 = SparseArray[Band[{# - 1 + Length@array0[[#]], #}, Automatic, {-1, 1}] -> 
 array0[[#]] & /@ {1, 2, 3}, {5, 5}]; 
array1 // MatrixForm

enter image description here

Update: Generalizing for arbitrary matrix input:

rttF = Function[{mat}, With[{dims = Dimensions[mat]},
 SparseArray[Band[{# - 1 + Last@dims, #}, Automatic, {-1, 1}] -> mat[[#]] & /@ 
 Range[First@dims], {Total@dims - 1, Total@dims - 1}]]]

Examples:

 rttF@array0 // MatrixForm

enter image description here

mtrx = Partition[CharacterRange["a", "x"], 4];
Row[MatrixForm@rttF[mtrx[[;; #, All]]] & /@ {2, 3, 4, 5, 6}]

enter image description here

Row[MatrixForm@rttF[mtrx[[All, ;; #]]] & /@ {2, 3, 4}]

enter image description here

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading