6
$\begingroup$

I have an $n\times n$ matrix G with elements g[i,j] that I construct with Table[g[i, j], {i, 1, n}, {j, 1, n}]

I have tried without success to write a code without loops in Mathematica that rearranges these elements to a vector in a certain way.

To illustrate the case $n=4$: $$ G=\begin{pmatrix} g[1,1]&g[1,2]&g[1,3]&g[1,4]\\g[2,1]&g[2,2]&g[2,3]&g[2,4]\\g[3,1]&g[3,2]&g[3,3] &g[3,4]\\g[4,1]&g[4,2]&g[4,3]&g[4,4]\end{pmatrix} \longrightarrow \begin{pmatrix} g[1,1]-g[2,2]\\g[2,2]-g[3,3]\\g[3,3]-g[4,4]\\g[1,2]-g[2,3]\\g[2,3]-g[3,4]\\g[2,1]-g[3,2]\\g[3,2]-g[4,3] \end{pmatrix} = \widetilde{G} $$

I.e, I'm trying to find a way to create a vector where every element is a subtraction between two diagonally consecutive elements in $G$, starting on the central diagonal, working its way to the right and then starting over under the central diagonal working its way to the left. For a fixed $n$ this could easily be done manually, but with my limitations in Mathematica I have not found a way to do this with arbitrary integers.

$\endgroup$
2
  • 3
    $\begingroup$ Ponder on Join @@ Table[-Differences[Diagonal[G, k]], {k, {0, 1, -1}}]. $\endgroup$ Jun 3, 2013 at 17:10
  • $\begingroup$ @0x4A4D : Exactly what I was looking for. Thank you! $\endgroup$
    – guest
    Jun 3, 2013 at 17:21

3 Answers 3

2
$\begingroup$
k = Table[g[i, j], {i, 4}, {j, 4}]; 
Flatten[Diagonal[k[[;; -2, ;; -2]] - k[[2 ;;, 2 ;;]], #] & /@ {0, 1, -1}]
$\endgroup$
4
$\begingroup$

Since it seems to suit the OP's needs:

G = Array[g, {4, 4}];
Join @@ Table[-Differences[Diagonal[G, k]], {k, {0, 1, -1}}]
   {g[1, 1] - g[2, 2], g[2, 2] - g[3, 3], g[3, 3] - g[4, 4], g[1, 2] - g[2, 3],
    g[2, 3] - g[3, 4], g[2, 1] - g[3, 2], g[3, 2] - g[4, 3]}

The key here is the use of Diagonal[] to extract the diagonals of a matrix (note the second argument for picking which diagonal to extract), and the use of Differences[] to generate the needed entries; however, the sign is opposite from what the OP needed, so we perform a negation afterwards. Join[] merely strings together the three diagonals thus extracted into a single list.

$\endgroup$
1
$\begingroup$

Partition with its undocumented 6-argument form:

gG = Array[g, {4, 4}];
Join @@ (Partition[#, 2, 1, None, {}, Subtract] & /@
   (Diagonal[gG, #] & /@ {0, 1, -1})) // MatrixForm // TeXForm

$\left( \begin{array}{c} g(1,1)-g(2,2) \\ g(2,2)-g(3,3) \\ g(3,3)-g(4,4) \\ g(1,2)-g(2,3) \\ g(2,3)-g(3,4) \\ g(2,1)-g(3,2) \\ g(3,2)-g(4,3) \\ \end{array} \right)$

BlockMap

Join @@ (BlockMap[Subtract @@ # &, #, 2, 1] & /@ 
  (Diagonal[gG, #] & /@ {0, 1, -1}))

$\left( \begin{array}{c} g(1,1)-g(2,2) \\ g(2,2)-g(3,3) \\ g(3,3)-g(4,4) \\ g(1,2)-g(2,3) \\ g(2,3)-g(3,4) \\ g(2,1)-g(3,2) \\ g(3,2)-g(4,3) \\ \end{array} \right)$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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